C# 同名的控制器方法
我有一个调用其他两个方法的方法,但当一个用户执行该代码时,我出现了一个错误C# 同名的控制器方法,c#,asp.net-mvc,methods,controller,redirecttoaction,C#,Asp.net Mvc,Methods,Controller,Redirecttoaction,我有一个调用其他两个方法的方法,但当一个用户执行该代码时,我出现了一个错误 public ActionResult CreateOrder(string action, string type) { /*Some Code*/ if(MyObject.isOk){ return RedirectToAction("EditOrder", new { code = ErrorCode, libelle = Description })
public ActionResult CreateOrder(string action, string type)
{
/*Some Code*/
if(MyObject.isOk){
return RedirectToAction("EditOrder", new { code = ErrorCode, libelle = Description });
}else{
return RedirectToAction("EditOrder", new { nordre = NoOrdre });
}
}
public ActionResult EditOrder(string nordre)
{
}
[ActionName("EditOrder")]
public ActionResult EditOrderError(string code, string libelle)
{
}
但是我得到一个404,因为脚本试图找到“EditOrderError”视图。ASP.NET MVC不允许您这样做,除非他们这样做 假设您使用的是C#4,一种可能的解决方法(尽管不是很好)是在单个控制器操作上使用可选参数:
public ActionResult EditOrder(string nordre = null, string code = null, string libelle = null)
{
if (nordre != null)
{
// ...
}
else if (code != null && libelle != null)
{
// ...
}
else
{
return new EmptyResult();
}
}
不能使用相同的HTTP方法/动词(GET/POST/etc)重载控制器操作
如果我需要控制器操作具有.NET不允许在标识符中使用的字符,我只会使用
ActionNameAttribute
。就像使用破折号(/controller/create user) 如何从视图中调用此方法?