Error: the name "Url" does not exist in the current context in asp.net mvc3

I added the Razor type MVC 3 view page to the root of an existing ASP.NET MVC 3 project. ViewName: TestView.cshtml I opened this file and tried to add the following line of code to 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 get an error: the name "Url" does not exist in the current context. But when I try to do the same in the view in the "Home" or "Account" folder, it works fine for me. Can someone help me find out exactly what I am missing?

+5
source share
1 answer

This is because you created the view page in the root of the MVC project, and not in the Views folder.

In the "Views" folder there is a web.config , 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, so you get the error "Url does not exist."

You can try copying the contents of web.config to the root directory "web.config", but I'm not sure if this is a good idea. And after that you can get a warning that the registered assembly provider is not registered, but according to this link it will work fine.

I believe that you may need to register a new custom RazorViewEngine to specify new paths to search for the as the default view engine searches for views in the Views` folder.

I'm not sure you might need to make any other changes, but if you do not need the “Views” in the root folder, I would recommend that you move your view to the Views folder, so I hope to let you work.

+17
source

Source: https://habr.com/ru/post/1208038/


All Articles