3

I am having an issue with html.actionlink. I have two routes defined:

routes.MapRoute(
                "AdminDefault",
                "Admin/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Page", action = "Details", id = "0" }
            );

the actionlink is using the AdminDefault, when I want it to use the default controller. How do I get it to use default route without using a routelink?

To add more info. I have a simple action (home/index), it shows a view (index.cshtml), in that view I have:

@Html.ActionLink("About", "About", "Home")

Which generates a url of "/Admin/Home/About" when I want it to be "/Home/About" the defaultadmin route is so that i can create links later that go to /admin/controller/action for any administration pages, but I want the default generated urls to not be under admin.

1 Answer 1

2

If you use Html.ActionLink from a view that is in an area then by default it will pass the area in to that link.

To override this you can call Html.ActionLink like so:

@Html.ActionLink("linkText", "actionName", "controllerName", new {Area = "AreaName"}, null);

where 'new {Area = "AreaName"}' are the route object values and 'null' are the html attributes.

You can therefore use 'new {Area = ""}'

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

1 Comment

Ah, I misunderstood. In that case, you will either have to swap your existing rules and then manually create the routes for the admin pages. Or you can add an admin area.

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.