Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing to the actions with same names but different parameters

Tags:

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

like image 560
zerkms Avatar asked Apr 12 '10 23:04

zerkms


People also ask

Can we have two methods with same name in controller?

To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods.

Can one action method have multiple views?

Yes, completely possible. And, it can be multiple views, or even a FileResult or other result type.

Can we define multiple route constraints for a route parameter?

We can apply the multiple constraints to a parameter by a colon (:) separator. [Route(URLPath/{parameterName: constrain:Constrain:….})] In the preceding example, the route will only be selected if the parameter id is an integer as well as a value of id greater than 1000.

Can we map multiple URLs to the same action with example?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });


1 Answers

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; }  }  
like image 155
Tommy Avatar answered Oct 01 '22 16:10

Tommy