ASP.NET-MVC . How to get the controller name from an url? -


How do I get the name of the controller of a relative URL using the routes defined in Global.asax?

Example:

If I have defiend like this in the way:

  routes.MapRoute ("default", // root name URL with new {controller = "home", action = "index", id = "", language = "n"}, "{language} / {controller} / {action} / {id} / Code>  

From the string "~ / en / products / list" I want the products (the name of the controller). Is there an existing method that already does?

Stephen Walther's blog View this project

This project is an old system in MV CAPEX.Veb. Abstract Reference.So replace it with a new one and replace this project again to get MvcFakes.dll.

This is my code:

  public string getContro LlerNameFromUrl () {rootclosure RC = new route compilation (); MvcApplication.RegisterRoutes (RC); System.Web.Routing.RouteData rd = New route data (); var reference = new FakeHttpContext ("\\" + HttpContext.Request.Url.AbsolutePath); Rd = rc.GetRoute data (reference); Return rd.Values ​​["Action"] ToString (); }  

My code above "MvcApplication" is the class name in Global.asax. <

Good luck!


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -