开发者

Make three-part route url for controller in ASP.NET MVC3

I am using ASP.NET MVC3 and curious about how to make routes like /Account/Ajax/Action map to controller AccountAjaxController method Action

Is there any way to do so?

Already solved issue using following code in Global.asax.cs file:

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new {controller = "Home", action = "Index", id = UrlParameter.Optional},
            new[] {"MyProjectName.FrontEnd.Web.Logic.Controllers"}
            );

        routes.MapRoute(
            "Ajax",
            "Ajax/{c开发者_StackOverflow中文版ontroller}/{action}/{id}",
            new[] {"MyProjectName.FrontEnd.Web.Logic.Controllers.Ajax"}
            );


One way that I've done this is with RouteCollection.MapRoute(). See http://msdn.microsoft.com/en-us/library/dd470521.aspx. You can do all kinds of stuff with routes.

In the code below I would typically call this from my global.asax.cs Application_Start():

    RouteCollection routes = RouteTable.Routes;

    routes.MapRoute(
        "ArbitraryRouteName", // Route name
        "Account/Ajax/Action/", // URL with parameters
        new
        {
            controller = "AccountAjax",
            action = "Action"
        },  // Parameter defaults
        new
        {
        }
    );

There are many ways to use this in order to generate links. You can use Html.RouteLink() in your views as an example. I believe that you can also call Html.ActionLink("link text", "AccountAjax", "Action"). I think that providing there is only one Route mapped to your controller and action name, ASP.NET MVC automatically figures out the proper URL to generate from your route mappings.

See http://msdn.microsoft.com/en-us/library/dd492585.aspx for an example of the RouteLink extension method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜