Mvc.net routing: routevalues ​​in maproutes

I need URLs like / controller / verb / noun / id, and my verb action will be the verb + noun. for example, I want / home / edit / team / 3 to apply an action method

public ActionResult editteam(int id){} 

I have the following route in my global.asax file.

 routes.MapRoute( "test", "{controller}.mvc/{verb}/{noun}/{id}", new { docid = "", action = "{verb}"+"{noun}", id = "" } ); 

The URLs match the route correctly, but I don’t know where I should build the action parameter called the name of the action method being called.
considers

+1
source share
2 answers

Try:

 public class VerbNounRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { IRouteHandler handler = new MvcRouteHandler(); var vals = requestContext.RouteData.Values; vals["action"] = (string)vals["verb"] + vals["noun"]; return handler.GetHttpHandler(requestContext); } } 

I don’t know how to automatically connect it for all routes, but in your case you can do this for this entry, for example:

 routes.MapRoute( "test", "{controller}.mvc/{verb}/{noun}/{id}", new { docid = "", id = "" } ).RouteHandler = new VerbNounRouteHandler(); 
+3
source

Try the following: use this route as a route:

 routes.MapRoute( "HomeVerbNounRoute", "{controller}/{verb}/{noun}/{id}", new { controller = "Home", action = "{verb}"+"{noun}", id = UrlParameter.Optional } ); 

Then you just put your actionresult method:

 public ActionResult editteam(int id){} 

Into Controllers / HomeController.cs and will be called when the URL matches the provided route.

-1
source

All Articles