Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can <a href="http://blog.slaks.net/2011/09/using-default-controller-in-aspnet-mvc.html" rel="nofollow">create a route that is constrained to only match actions in your <code>Customer</code> controller</a>.</p> <pre><code>public static class RoutingExtensions { ///&lt;summary&gt;Creates a route that maps URLs without a controller to action methods in the specified controller&lt;/summary&gt; ///&lt;typeparam name="TController"&gt;The controller type to map the URLs to.&lt;/typeparam&gt; public static void MapDefaultController&lt;TController&gt;(this RouteCollection routes) where TController : ControllerBase { routes.MapControllerActions&lt;TController&gt;(typeof(TController).Name, "{action}/{id}", new { action = "Index", id = UrlParameter.Optional }); } ///&lt;summary&gt;Creates a route that only matches actions from the given controller.&lt;/summary&gt; ///&lt;typeparam name="TController"&gt;The controller type to map the URLs to.&lt;/typeparam&gt; public static void MapControllerActions&lt;TController&gt;(this RouteCollection routes, string name, string url, object defaults) where TController : ControllerBase { var methods = typeof(TController).GetMethods() .Where(m =&gt; !m.ContainsGenericParameters) .Where(m =&gt; !m.IsDefined(typeof(ChildActionOnlyAttribute), true)) .Where(m =&gt; !m.IsDefined(typeof(NonActionAttribute), true)) .Where(m =&gt; !m.GetParameters().Any(p =&gt; p.IsOut || p.ParameterType.IsByRef)) .Select(m =&gt; m.GetActionName()); routes.Add(name, new Route(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults) { { "controller", typeof(TController).Name.Replace("Controller", "") } }, Constraints = new RouteValueDictionary { { "action", new StringListConstraint(methods) } } }); } private static string GetActionName(this MethodInfo method) { var attr = method.GetCustomAttribute&lt;ActionNameAttribute&gt;(); if (attr != null) return attr.Name; return method.Name; } class StringListConstraint : IRouteConstraint { readonly HashSet&lt;string&gt; validValues; public StringListConstraint(IEnumerable&lt;string&gt; values) { validValues = new HashSet&lt;string&gt;(values, StringComparer.OrdinalIgnoreCase); } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return validValues.Contains(values[parameterName]); } } #region GetCustomAttributes ///&lt;summary&gt;Gets a custom attribute defined on a member.&lt;/summary&gt; ///&lt;typeparam name="TAttribute"&gt;The type of attribute to return.&lt;/typeparam&gt; ///&lt;param name="provider"&gt;The object to get the attribute for.&lt;/param&gt; ///&lt;returns&gt;The first attribute of the type defined on the member, or null if there aren't any&lt;/returns&gt; public static TAttribute GetCustomAttribute&lt;TAttribute&gt;(this ICustomAttributeProvider provider) where TAttribute : Attribute { return provider.GetCustomAttribute&lt;TAttribute&gt;(false); } ///&lt;summary&gt;Gets the first custom attribute defined on a member, or null if there aren't any.&lt;/summary&gt; ///&lt;typeparam name="TAttribute"&gt;The type of attribute to return.&lt;/typeparam&gt; ///&lt;param name="provider"&gt;The object to get the attribute for.&lt;/param&gt; ///&lt;param name="inherit"&gt;Whether to look up the hierarchy chain for attributes.&lt;/param&gt; ///&lt;returns&gt;The first attribute of the type defined on the member, or null if there aren't any&lt;/returns&gt; public static TAttribute GetCustomAttribute&lt;TAttribute&gt;(this ICustomAttributeProvider provider, bool inherit) where TAttribute : Attribute { return provider.GetCustomAttributes&lt;TAttribute&gt;(inherit).FirstOrDefault(); } ///&lt;summary&gt;Gets the custom attributes defined on a member.&lt;/summary&gt; ///&lt;typeparam name="TAttribute"&gt;The type of attribute to return.&lt;/typeparam&gt; ///&lt;param name="provider"&gt;The object to get the attribute for.&lt;/param&gt; public static TAttribute[] GetCustomAttributes&lt;TAttribute&gt;(this ICustomAttributeProvider provider) where TAttribute : Attribute { return provider.GetCustomAttributes&lt;TAttribute&gt;(false); } ///&lt;summary&gt;Gets the custom attributes defined on a member.&lt;/summary&gt; ///&lt;typeparam name="TAttribute"&gt;The type of attribute to return.&lt;/typeparam&gt; ///&lt;param name="provider"&gt;The object to get the attribute for.&lt;/param&gt; ///&lt;param name="inherit"&gt;Whether to look up the hierarchy chain for attributes.&lt;/param&gt; public static TAttribute[] GetCustomAttributes&lt;TAttribute&gt;(this ICustomAttributeProvider provider, bool inherit) where TAttribute : Attribute { if (provider == null) throw new ArgumentNullException("provider"); return (TAttribute[])provider.GetCustomAttributes(typeof(TAttribute), inherit); } #endregion } </code></pre>
 

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