Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to limit yourself to only on possible controller you can do this:</p> <pre><code>routes.MapRoute( "Default", // Route name "{category}/{action}/{id}", // URL with parameters new { controller = "Categories", category="DefaultCategory", action = "Index", id = UrlParameter.Optional } ); </code></pre> <p>If you do it this way you can address different controllers:</p> <pre><code>routes.MapRoute( "Default", // Route name "{controller}/{category}/{id}/{action}", // URL with parameters new { controller = "Home", category="DefaultCategory", action = "Index", id = UrlParameter.Optional } ); </code></pre> <p>Based on your comment you would have do go in this direction:</p> <pre><code>routes.MapRoute( "Categories", "{category}/{action}", new { controller = "Categories", action = "Index" }, new { category= new FromValuesListConstraint("Category1", "Category2", "Category3", "Category4", "Category5") } ); // all your default routing routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); </code></pre> <p>FromValuesListContraint is only a sample for the simplest case if you have only a few categories. If you have dynamic categories that come out of a database you have to do a different implementation, but still can follow my example with IRoutecontraints.</p> <p>Here is the Sample IRouteConstraints Implementation (FromValuesListConstraint):</p> <pre><code>public class FromValuesListConstraint : IRouteConstraint { private List&lt;string&gt;_values; public FromValuesListConstraint(params string[] values) { this._values = values.Select(x =&gt; x.ToLower()).ToList(); } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { string value = values[parameterName].ToString(); return _values.Contains(value.ToLower()); } } </code></pre> <p>The reason why you have to do this whole IRoutConstraint thing here is otherwise you would not be able to use you default route because a request for www.mysite.com/mycontroller or www.mysite.com/mycontroller/myaction would match the categories route.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload