14

I added a MVC 3 View Page of type Razor to the root of the existing ASP.NET MVC 3 project. ViewName: TestView.cshtml I opened the above file and tried to add the following line of code in the view:

<head>
<link href="@Url.Content("~/Content/Styles/Themes/base/jquery.ui.all.css")" rel="Stylesheet" type="text/css" />
    <title></title>
</head>

For the line above I am getting an error: The name 'Url' does not exist in the current context. But when I am trying the same in a view within the Home or Account folder it is working fine for me. Can any one help me to know what exactly I am missing?

2 Answers 2

32

This is because you have created the view page in the root of the MVC project rather than in the Views folder.

In the Views folder there is a web.config file which has the following section:

<system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            ...

The namespaces specified here are used to compile the views which is why you get the 'Url does not exist' error message.

You could try to copy the contents of the web.config to the 'web.config' root but I'm not sure this is a good idea. And after doing so you may get a warning that there is no build provider registered but according to this link this will work perfectly fine.

I believe you may then have to register a new custom RazorViewEngine to specify the new paths to search for viewas the default view engine searches for views in theViews` folder.

I'm not sure if you may need to make any other changes but unless you need to have Views in the root folder, I would recommend you move your view to the Views folder to hopefully allow you to get it working.

3
  • When I upgraded to next version I needed to manually edit & change to the newer version, System.Web.Mvc, Version=5.2.7.0 and System.Web.WebPages.Razor, Version=3.0.0.0 in \Views\Web.config, so that the IntelliSense shouldn't show false errors in the razor .cshtml file.
    – stomy
    Sep 10, 2020 at 20:30
  • @Dangerous If I could I would give you a huge smack on the face!!! kkkkk Thank you!!! Nov 19, 2020 at 18:56
  • @Marco Alves - I don't know what to say! I'm flattered! Thanks! :-)
    – Dangerous
    Nov 21, 2020 at 10:57
0

try to check your packages.config make sure it has same version with package version in webconfig.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.