0

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.

2
  • I've resolved all the "does not exist in the current context" issues, the version numbers in 1 of the web.config files was incorrect. Just need to figure out why my @Url.Actions don't render to a href= on this page. Commented Mar 6, 2016 at 14:44
  • Tried changing the anchor and Url.Action Html.ActionLink, very similar result, the only difference is that whereas anchor and Url.Action results in an anchor with no href at all, Html.ActionLink gives an anchor with an empty href. Commented Mar 7, 2016 at 6:44

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

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.