简体   繁体   English

控制器方法具有相同名称

[英]Controller Methods with same name

I have this method who call 2 others methods but i've an error when a execute that code. 我有这个方法调用其他两个方法,但我执行代码时出错。

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)
{

    }

But i get an 404 because the script try to find the "EditOrderError" view. 但我得到一个404因为脚本试图找到“EditOrderError”视图。

ASP.NET MVC doesn't allow you to overload controller actions unless they handle different HTTP verbs . ASP.NET MVC不允许您重载控制器操作, 除非它们处理不同的HTTP谓词

Assuming you're using C# 4, one possible workaround, albeit not a pretty one, is to use optional parameters on a single controller action: 假设您正在使用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();
    }
}

You cannot overload controller actions using the same HTTP Method/Verb (GET/POST/ etc) 您不能使用相同的HTTP方法/动词(GET / POST / etc)重载控制器操作

I would only use ActionNameAttribute if I need the controller action to have characters that .NET doesn't allow in an identifier. 如果我需要控制器操作来使用.NET在标识符中不允许的字符,我只会使用ActionNameAttribute Like using dashes (/controller/create-user). 喜欢使用破折号(/ controller / create-user)。 Like this one. 像这个。 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM