ASP.NET MVC Routes: Defining a Route, Including Subpaths

Let's say I want to redirect all requests in / js / * to the Index JavaScript controller. In other words, these routes should call JavaScriptController.Index() :

 /js/root/index.css /js/user/account/index.css /js/master.css 

What will be the route definition in my Global.asax.cs file?

This does not work:

 routes.MapRoute("JavaScript", "js/{*path}", new { controller = "JavaScriptController", action = "Index" }); 

The breakpoint is never called during debug mode with:

 public class JavaScriptController : Controller { [HttpGet] public void Index(string path) { var browser = HttpContext.Request.Browser; System.Diagnostics.Debugger.Break(); } } 

Did I miss something?

+4
source share
1 answer

You should have a short controller name in the default values โ€‹โ€‹of the ie route:

  routes.MapRoute( "JavaScript", "js/{*path}", new { controller = "javascript", action = "Index" } ); 

It will work.

+3
source

Source: https://habr.com/ru/post/1334274/


All Articles