Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create a new Route which overrides the <code>GetVirtualPath</code> method. In this method you add a trailing slash to the <code>VirtualPath</code>. Like this:</p> <pre><code>public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { VirtualPathData path = base.GetVirtualPath(requestContext, values); if (path != null) path.VirtualPath = path.VirtualPath + "/"; return path; } </code></pre> <p>For brevity I posted the <a href="http://codepaste.net/exq9mg" rel="noreferrer">whole example on CodePaste.net</a></p> <p>Now all you have to do is register the routes with <code>routes.MapRouteTrailingSlash()</code> instead of <code>routes.MapRoute()</code>.</p> <pre><code>routes.MapRouteTrailingSlash("register", "register", new {controller = "Users", action = "Register"} ); </code></pre> <p>The route will then add a slash to the path when the <code>GetVirtualPath()</code> is called. Which <code>RedirectToAction()</code> will do.</p> <p><strong>Update:</strong> Because the CodePaste link is down, here is the full code:</p> <pre><code>public class TrailingSlashRoute : Route { public TrailingSlashRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) {} public TrailingSlashRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) {} public TrailingSlashRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) {} public TrailingSlashRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) {} public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { VirtualPathData path = base.GetVirtualPath(requestContext, values); if (path != null) path.VirtualPath = path.VirtualPath + "/"; return path; } } public static class RouteCollectionExtensions { public static void MapRouteTrailingSlash(this RouteCollection routes, string name, string url, object defaults) { routes.MapRouteTrailingSlash(name, url, defaults, null); } public static void MapRouteTrailingSlash(this RouteCollection routes, string name, string url, object defaults, object constraints) { if (routes == null) throw new ArgumentNullException("routes"); if (url == null) throw new ArgumentNullException("url"); var route = new TrailingSlashRoute(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; if (String.IsNullOrEmpty(name)) routes.Add(route); else routes.Add(name, route); } } </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