开发者

mvc.net routing: routevalues in maproutes

开发者 https://www.devze.com 2023-01-17 11:12 出处:网络
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

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 constru开发者_如何学编程ct the action parameter that is name of action method being called.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消