Microsoft.Web.Helpers not visible in the view - asp.net-mvc

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>

Related

Razor page does not contain definintion of Html helper class

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.

VS 2013 MVC 5 No Intellisense for HtmlHelpers in Area Views

Project: VB.NET MVC5 w/ EF6, MvcScaffolding, and BootstrapEditorTemplates
Since this morning I don't get any intellisense for any of the built in MVC helpers, nor my custom ones in any of the Views in an Area.
NOTE: I can still see Html.Encode, and Htm.DropDownList (NOT DropDownListFor<>) etc.
If I build it I recieve errors, though I can still browse the application correctly and see the correct editors and data.
#ModelType Quotes4YouVB.Request
#Html.EditorFor(Function(model) model.Urgency)
#Html.EditorFor(Function(model) model.Message)
I recieve the following errors among other similar ones.
'ModelType' is not declared. It may be inaccessible due to its
protection level. 'ViewBag' is not declared. It may be inaccessible
due to its protection level. 'EditorFor' is not a member of
'System.Web.WebPages.Html.HtmlHelper'. 'ValidationMessageFor' is not a
member of 'System.Web.WebPages.Html.HtmlHelper'.
I've tried all the usual suspects:
Close and Re-open View
Restart VS
Delete /bin and /obj
Tools / Imports and Export Settings / Reset All Settings
Uninstall MvcScaffolding, T4Scaffolding and T4Scaffolding.Core
Re-Installed MVC 5.2 to project through NuGet
NuGet: Update-Package
Any ideas anyone? It seems odd that it's specific to /Areas/, it works completely fine in ~/Views/X/Y.vbhtml
Found the solution! I'm unsure why this specific thing causes this problem, but as with most VS issues I'm not gonna question it too much, just gonna go with it haha.
It relates to the System.Web.WebPages.Razor Host FactoryType section.
My ~/Views/Web.config has the following:
<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>
...
</namespaces>
</pages>
</system.web.webPages.razor>
Specifically the Version=5.0.0.0 part. Because I'd just re-installed MVC 5.2.x from NuGet, so surely it should be 5.2.0.0?
I checked the Web.config in my ~/Areas/[AreaName]/Views/Web.Config and found that it had everything the same, except it read Version=5.2.0.0:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
I tried both in both files and found that wherever 5.0.0.0 is used then all intellisense works perfectly for those views.
So, the solution: Modify all Web.config files to use a compatible version for the factoryType. (5.0.0.0 works for me, though 5.2.0.0 does not)
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<!--<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.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="System.Web.Optimization" />
</namespaces>
</pages>
</system.web.webPages.razor>
I'm really unsure about why this is happening though, if anyone has any further information or a better solution to use 5.2.0.0 then please share :)
I once had this problem. This solved my issue:
Open project's output folder in Windows explorer (usually bin folder at the root of the project) and delete everything in it. Then close the solution and reopen it.
Hope that helps.

How to share web.config file between MVC 3 and MVC 4 Project

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

Razor Compiler Warning/Errors - ASP.NET MVC 4

I have an issue which seems to have been reported here:
Need razor view engine auto-complete to work in a class library?
My issue is the following Warning:
G:\Accountable\Accountable\Views\LedgerUser\EditorTemplates\LedgerServiceViewModel.cshtml:
ASP.NET runtime error: There is no build provider registered for the
extension '.cshtml'. You can register one in the
section in machine.config or web.config.
Make sure is has a BuildProviderAppliesToAttribute attribute which
includes the value 'Web' or 'All'.
This issue is apparent in all my views. Sample images below.
Now all posts and references online that I have found are referencing to adding various Web.config (as mentioned in the previous post) options, but this doesn't seem to work, as I have these configurations defaulted in the project. Here are my Web Configs:
Main Web.config
View root Web Config
I think this issue maybe while im also getting the following compiler errors on the Views:
The name 'Html' does not exist in the current context
What am I doing wrong?
Arrrgghhh i had missed the following from the ROOT Web.config:
<compilation debug="true" targetFramework="4.0">
<!-- New -->
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<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>
</compilation>
This allowed fixed the error and allowed me to use Intellisense within the View.
Putting back the following lines in root Web.config fixed:
<system.web>
<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.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
I got it working by simply unloading and reloading the project. All the namespaces above were already there in the root web.config and I was still getting this issue.
Removing the temporary asp.net framework files works for me.
Stop IIS and remove everything under this folder:
C:\Windows\Microsoft.NET\Framework64\[framework version]\Temporary ASP.NET Files
Then restart IIS. Bam!
I tried by restart IIS, removed Temporary ASP.NET Files and changing the compilation tag in the web config file. None of them work for me.
I fixed the issue by removing and adding System.Web.MVC to the project.
In my case Razor syntax are Highlighted in red.

Developing ASP.NET MVC using MonoDevelop on MAC

I am developing a web app using ASP.NET MVC 2.0 in MonoDevelop. I have latest Mono 2.10 and MonoDevelop 2.4.2 and I am running them on Mac.
According to mono’s release notes, it should support ASP.NET MVC 2.0. However, I found MonoDevelop’s project templates still only support ASP.NET MVC 1.0. So I had to create MVC project from MonoDevelop and remove system.web.mvc 1.0.0.0 from reference and add system.web.mvc 2.0.0.0 back in. I can write and compile code OK and MonoDevelop does provide intellisense for methods only exist in MVC 2.0 from both C# and aspx file. However, when I trying to run it from MonoDevelop, I had Compilation error complaining ”<%: Html.LabelFor(model => model.Name) %>” as LabelFor method cannot be recognized .
MonoDevelop uses XSP as its testing web server. I am not sure how it configured as I didn’t install it myself. I think it was installed as part of MonoDevelop. My guess is XSP somehow still uses MVC 1.0 but I can't prove it as I couldn’t even find where it is installed.
Could anyone help me out please?
Update:
I have been suspecting web.config too. Here are how they look like now:
web.config
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
<customErrors mode="RemoteOnly">
</customErrors>
<pages>
<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>
<authorization>
<allow users="*" />
</authorization>
web.config under views
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
</httpHandlers>
<pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Setting up an ASP.NET project for ASP.NET MVC requires changing and adding a load of settings in web.config. The project template in MD is for ASP.NET MVC 1.0, not ASP.NET MVC 2, so you're probably missing some of the necessary settings, maybe a namespace import.
I have this running with two of my current projects on MacOS X. There were only three steps I needed to make sure were done, they are as follows:
Upgrade the version on all references in both the inner (views) web config and the outter(app) [you've done this].
Dereference System.Web.MVC 1.0.0.0 & Reference 2.0.0.0 for the project
Ensure you don't have 1.0.0.0 binaries in your bin path, the public key does not change
I've done a downloadable zip file of a project template for Asp.Net MVC 4 which builds and runs on/in Mono/Xamarin Studio/MonoDevelop on Mac & Linux at http://www.cafe-encounter.net/p1319/run-asp-net-mvc4-on-mono-monodevelop-on-mac-the-c-template-project.
I should add that a lot of the issue resolution came from Does the Razor View Engine work for Mono?

Resources