Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同名的控制器方法_C#_Asp.net Mvc_Methods_Controller_Redirecttoaction - Fatal编程技术网

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)

如何从视图中调用此方法?