0

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/{controller}/{action}/{id}",
            new[] {"MyProjectName.FrontEnd.Web.Logic.Controllers.Ajax"}
            );

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.