M Kenyon II
M Kenyon II

Reputation: 4274

Can a Url.Action specify a route in MVC5?

In our default layout we have our logo wrapped with a URL.Action to navigate back to the home screen. However, if we are in an Area, it tries to navigate to the home controller in that area, which doesn't exist.

Our home controller is Submission. Here is the link in the layout file:

<a class="navbar-brand custm-navbar-brand" href="@Url.Action("Index", "Submission")">
    <img src="@Url.Content("~/Content/images/eed-main-logo.png")" alt="E-Editorial Discovery" width="335" height="56">
</a>

If I am in an area like this: /Admin/Security/Index
the above link tries to go to: /Admin/Submission/Index
but it should go to: /Submission/Index

I'm sure there's a better way, but I haven't found it yet.

Upvotes: 3

Views: 951

Answers (2)

webnoob
webnoob

Reputation: 15934

You could specify the area in the Url.Action call but this can get messy.

Rather than having to specify the area, why not sort the route out itself so it knows to tie it down to that namespace:

context.MapRoute("ExpressCheckoutRoute",
    "expresscheckout/stage/{stageName}/{id}",
    new { controller = "ExpressCheckout", action = "Stage", area = "FrontEnd", id = UrlParameter.Optional },
    new[] { "Web.Areas.FrontEnd.Controllers" }
    ).DataTokens["UseNamespaceFallback"] = false;

This sorted the issue out for me and I no longer have to specify the area paramter (take note of the last 2 parts to that mapping).

Upvotes: 1

Sean Hallinan
Sean Hallinan

Reputation: 116

Specify the area like you would a parameter. So your first line should be:

<a class="navbar-brand custm-navbar-brand" href="@Url.Action("Index", "Submission", new { Area = "AreaName" })">

Upvotes: 2

Related Questions