Can I not add my project namespace to the web.config so that the Razor view engine includes my project namespace for all pages in my project? Like so:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace = "MyProjectNamespace.NestedNamespace"/>
</namespaces>
</pages>
</system.web.webPages.razor>
It isn't working.
This is an old question, but apparently there is still a bit of a problem with Visual Studio (2015 2017 2019 as of right now). After adding the namespaces to the web.config file in your Views folder, close and re-open all views. The namespaces were not recognized and the views would continuously throw runtime errors until I closed them all.
The namespace needs to be added to the web.config file in your Views folder, not the root project's web.config. This will import the namespace for all cshtml files in that views folder only. If your project has MVC areas, each area will have its own Views folder and a separate views web.config file. To import the namespace into those views, you need to add the namespace to each area's view folder's web.config as well.
You can also add a #using statement to a _ViewStart.cshtml file which will cover all views in the same folder or any subfolders.
#using MyProjectNamespace.NestedNamespace
Related
I am currently working on an MVC project. The project is working fine on another colleagues machine but I am having trouble with this project. It constantly gives errors with Html helper classes.
"System.Web.WebPages.HtmlHelper does not contain a definition for 'TextboxFor' does not contain definition and no extension method 'TextBoxFor' accepting argument of type System.Web.WebPages.HtmlHelper could be found."
I have googled this problem but have not found any useful solution.
Here are some steps that I tried to solve this problem.
Clean solution and rebuild it.
removed bin and obj folder and rebuild the solution.
checked references in web.config files (both)
Added refrences in web.config like System.Web.Mvc etc..
Copied packages from other colleagues' machine.
Note: I am using visual studio 2013 with MVC 5
Sounds like you're missing the following in the Web.Config in the views folder:
/Views/Web.Config
<?xml version="1.0"?>
<configuration>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage"> // <-- this line and contents are important
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
Views typically derive from System.Web.Mvc.WebViewPage which is configured in the web.config. Make sure the version above matches the version you need.
I having this error but I cant find the way to help it
Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: 'System.Web.Mvc.WebViewPage' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
<add name="ApplicationInsightsWebTracking"
type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule,
Microsoft.AI.Web" />
</httpModules>
**<pages pageBaseType="System.Web.Mvc.WebViewPage">**
<namespaces>
<add namespace="GridMvc" />
Source File: C:\app\web.config Line: 32
It is clear that System.Web.Mvc.WebViewPage namespace added at the wrong place (as shown by httpModules used before the line causing problem). The correct place to add respective namespace is in a web.config file inside Views folder under system.web.webPages.razor element, as shown below:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="GridMvc" />
<!-- other namespaces -->
</namespaces>
</pages>
</system.web.webPages.razor>
The pages element under system.web is primarily intended for webforms project configuration (under System.Web.UI.Page namespace), which often contains 2 attributes (validateRequest & clientIDMode):
<pages validateRequest="true" clientIDMode="AutoID">
To use MVC version of pageBaseType attribute, you need to check if current pages element exists inside system.web.webPages.razor. In this case you added the namespace incorrectly under system.web, which doesn't inherit System.Web.Mvc namespace & throwing disallowed namespace error.
I have a MVC 3.0 and MVC 4.0 Projects side by side
I did this for the resource sharing between two solutions. it is works fine when i change the config file. changes in details.
In MVC 3.
<assemblies>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
When i remove the <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> MVC 4 is working fine. after i debug the project in MVC 3.0 it showing an error msg(The type or namespace name 'Ajax' does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)
) because of i remove the "System.Web.Mvc, Version=3.0.0.0" , if i have to run that means i have to place the refreance again. here i am doing everyting manually, is there is any way to automatic do this ?
For example, two xml files in server side i read that xml file and replace the config file.
Already Tried.
In MVC 4 project i used the WebConfigurationManager to remove the refreance it works , after i opened the MVC 3 it shows that error msg.
You could create a new build configuration for Mvc3 and run a pre-build event that will overwrite your web.config, it's an idea I've adopted from Hanselman.
If you follow the link the only difference for your set up would be that you would have a config called
Web.Config.Mvc3 (and possibly Web.Config.Mvc3Release)
and leave Mvc as your default builds
Web.Config.Debug
Web.Config.Release
When I install package Microsoft.Web.Helpers I get next warning txt file (after installing):
If the package is installed in an ASP.NET MVC 3 site, the site will not work.
If you’ve already installed the package into an MVC 3 application, uninstall it.
If the MVC 3 site does not work even after you have uninstalled the package, you might need to reinstall the ASP.NET MVC 3 packages as well.
I can use Microsoft.Web.Helpers in controller class but cant in the view (#using Microsoft.Web.Helpers not found).
I trying do next: Using ReCaptcha with MVC3 and razor? ReCaptcha and MVC3, problems getting Microsoft.Web.Helpers working .But it doesnt help.What I missed?
You're missing the namespace in Views\Web.config (not the app root web.config).
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
I have an existing asp.net web application which I need to convert to mvc3.
As soon as I add the below my project's web.config, the project stops working I am using IIS 5. I guess I have missed something in IIS, please advise
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
First confirm if any MVC3 project works with IIS5 the way you have it configured, you may need to setup the wildcard rule and uncheck verify if file exsists. Once you have done this what I do is just create a new MVC3 project that is empty and move over my stuff from the old project to the new project. That is the least hassel free way of doing it. Keep the namespace of the solution the same so you don't have to deal with namespace mismatch issues.