views:

86

answers:

2

Hello everyone,

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.
regards

A: 

Try the following, use this as your route:

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

Then you simply put your actionresult method:

public ActionResult editteam(int id){}

Into Controllers/HomeController.cs and that will get called when a URL matches the provided route.

JTrott
i m afraid that is default value for route parameters not the actual values of verb and noun assigned at runtime. using this route will result in {verb}{noun} value in action field of route value dictionary every time this route is matched.
Muhammad Adeel Zahid
is it possible you could use a custom controller mapped perhaps as you did with .MVC on the end, or even a hard string: /Custom/{verb}/{noun}/{id} with then as I say, the actionresults within your home controller? passed directly?if this is not the information you are looking for, please try to clarify the question as I'm struggling to understand what additional you could want to know.
JTrott
@JTrott what i need is if i have url like /edit/team/1 route value dictionary should contain editteam at action key. if i have url like manage/team/1 route value dictionary should have manageteam in action key. routes.Maproutes only can define defaults not the dynamic values of parameters (verbs and nouns in my case). i think solution given by eglasius should work.
Muhammad Adeel Zahid
+1  A: 

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();
eglasius