Search code examples
asp.net-mvc-2routesurl-routing

mvc.net routing: routevalues in maproutes


I need urls like /controller/verb/noun/id and my action methods would be verb+noun. For example I want /home/edit/team/3 to hit the action method

public ActionResult editteam(int id){}

I have following route in my global.asax file.

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


            );

URLs correctly match the route but I don't know where should I construct the action parameter that is name of action method being called.


Solution

  • 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 of a way to hook it for all routes automatically, but in your case you can do it for that entry like:

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