Tom
Tom

Reputation: 139

C# ASP.NET MVC @Url.Action does not exist in the current context

I know there can be some peculiarities in Visual Studio where it highlights @URL as not existing in the current context, but it still works. The problem I have however I think is different.

I am writing an application at the moment, I have completed numerous admin screens all within there own area. The main area until now only had a ViewStart pointing to a _Layout which detected if you were logged in or not displayed a login page (which works) if you aren't and a series of hyper links if you are that take you to the admin pages - which all work.

In the _Layout the @Url is highlighted as does not exist in the current context - but works fine.

Now I have started writing the main area, so have added an Index.cshtml, this has a @model which is highlighted as does not exist in the current context, but I am able to access the data within without issue.

The problem is any anchor I add in the page and set the URL using @Url.Action doesn't work, they all say does not exist in the current context and when you view the source of the page the href= is missing as it the URL.

If I manually code the href= it does appear in the source and does work.

Update:

So further to comment 2 below, I now have...

<div class="btn-toolbar">
    <a href="@Url.Action("new")" class="btn btn-primary btn-sm">
        <i class="glyphicon glyphicon-plus"></i>
        Create Company
    </a>
</div>

@Html.ActionLink("Create Company","New","Summary")

In my chtml page and...

<a href="@Url.Action("index", "Users", new {area = "Admin"})">User Admin</a>
<a href="@Url.Action("index", "Companies", new {area = "Admin"})">Customer Admin</a>

In my _Layout in which the cshtml is rendered, but the resulting html is...

<a href="/admin/Users">User Admin</a>
<a href="/admin/Companies">Customer Admin</a>
<a href="/logout">Logout</a>

<h2>
    Overview Screen
</h2>

<br/>

<div class="btn-toolbar">
    <a class="btn btn-primary btn-sm">
        <i class="glyphicon glyphicon-plus"></i>
        Create Company
    </a>
</div>

<a href="">Create</a>

So as you can see, the anchor and @Url.Action works fine in the _Layout, but neither the @Url.Action nor @Html.ActionLink generates a URL.

Upvotes: 0

Views: 5039

Answers (3)

Vincent Pazeller
Vincent Pazeller

Reputation: 1508

I had this problem, too.

I think this is related to Attribute Routing and routing precedence. This may happen if you have routes.MapMvcAttributeRoutes(); after a default routes.MapRoute definition in your RouteConfig.cs file.

You may want to move routes.MapMvvAttributeRoutes() before other routes.MapRoute definition.

Upvotes: 0

Fares Ali
Fares Ali

Reputation: 1

You should check routing. If ok then check plesk version, because if it is old or not updated then your pages will not working and this error will always appear.

Upvotes: 0

JAZ
JAZ

Reputation: 1070

Use single quotes around the @Url.Action code.

<a href='@Url.Action("new")' class="btn btn-primary btn-sm">


<a href='@Url.Action("index", "Users", new {area = "Admin"})'>User Admin</a>
<a href='@Url.Action("index", "Companies", new {area = "Admin"})'>Customer Admin</a>

Upvotes: 1

Related Questions