Routing to the actions with same names but different parameters

点点圈 提交于 2019-12-18 14:00:40

问题


I have this set of routes:

        routes.MapRoute(
            "IssueType",
            "issue/{type}",
            new { controller = "Issue", action = "Index" }
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

Here is the controller class:

public class IssueController : Controller
{
    public ActionResult Index()
    {
        // todo: redirect to concrete type
        return View();
    }

    public ActionResult Index(string type)
    {
        return View();
    }
}

why, when i request http://host/issue i get The current request for action 'Index' on controller type 'IssueController' is ambiguous between the following action methods:
I expect that first one method should act when there is no parameters, and second one when some parameter specified.

where did i made mistake?

UPD: possible duplicate: Can you overload controller methods in ASP.NET MVC?

UPD 2: due to the link above - there is no any legal way to make action overloading, is it?

UPD 3: Action methods cannot be overloaded based on parameters (c) http://msdn.microsoft.com/en-us/library/system.web.mvc.controller%28VS.100%29.aspx


回答1:


I would have one Index method that looks for a valid type variable

    public class IssueController : Controller  
{  
    public ActionResult Index(string type)  
    {  
        if(string.isNullOrEmpty(type)){
            return View("viewWithOutType");}
        else{
            return View("viewWithType");} 
    }
}

EDIT:

How about creating a custom attribute that looks for a specific request value as in this post StackOverflow

[RequireRequestValue("someInt")] 
public ActionResult MyMethod(int someInt) { /* ... */ } 

[RequireRequestValue("someString")] 
public ActionResult MyMethod(string someString) { /* ... */ } 

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute { 
    public RequireRequestValueAttribute(string valueName) { 
        ValueName = valueName; 
    } 
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) { 
        return (controllerContext.HttpContext.Request[ValueName] != null); 
    } 
    public string ValueName { get; private set; } 
} 



回答2:


I ran into a similar situation where I wanted my "Index" action to handle the rendering if I had an ID specified or not. The solution I came upon was to make the ID parameter to the Index method optional. For example, I originally tried having both:

public ViewResult Index()
{
    //...
}
// AND
public ViewResult Index(int entryId)
{
    //...
}

and I just combined them and changed it to:

public ViewResult Index(int entryId = 0)
{
    //...
}



回答3:


You can do it using an ActionFilterAttribute that checks the parameters using reflection (I tried it) but it's a bad idea. Each distinct action should have its own name.

Why not just call your two methods "Index" and "Single", say, and live with the limitation on naming?

Unlike methods that are bound at compile time based on matching signatures, a missing route value at the end is treated like a null.

If you want the [hack] ActionFilterAttribute that matches parameters let me know and I'll post a link to it, but like I said, it's a bad idea.




回答4:


All you have to do is mark your second Action with [HttpPost]. For instance:

public class IssueController : Controller
{
    public ActionResult Index()
    {
        // todo: redirect to concrete type
        return View();
    }

    [HttpPost]
    public ActionResult Index(string type)
    {
        return View();
    }
}


来源:https://stackoverflow.com/questions/2626179/routing-to-the-actions-with-same-names-but-different-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!