Precompile not working with Razor views in MVC 4 - asp.net-mvc-4

I'm trying to precompile razor views but during the build VS2010 throws errors such as "#ViewBag does not exist in the current context." The project builds and runs fine when views are not precompiled.
In order to turn on view compilation I set the following node in the project file:
<MvcBuildViews>true</MvcBuildViews>
The section below exists in the Views Web.config file.
<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.Routing" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Xml"/>
<add namespace="System.Xml.Linq"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Any ideas why the project won't build?

Geeze, do I ever feel like an idiot. Turns out there were some cshtml files in a _temp folder that resided inside of the project folder. Even though the folder was excluded from the solution the compiler was picking up the files contained therein. Moving that _temp file out of the project folder fixed the issue.

Related

VS Community - VB - Unable to read to or write from app.config via Settings dialog

Recently I posted a couple of errors I was having, relating to app.config in a VB project. Andrew Morton kindly pointed me to an existing answer which resolved the first part of the issue, but I've since found that I still can't add/update variables in app.config via the Settings dialog in the project properties, nor can the Settings dialog read the app.config file.
The error message is:
an error occurred while reading the app.config file. the file might be corrupted
If I manually change the variable in app.config the program I'm coding still finds the old value as it's taking it from Settings.Designer.vb, and that's not getting updated when I change the variable in Settings as the platform isn't reading the app.config.
I've (again) run the app.config through an XML checker, all fine. I read one solution for this issue, which suggested the cause can be duplicate elements in the 'sectionGroup' area of app.config, but mine doesn't appear to have duplicate elements (app.config posted).
Steps I have tried are:
Removed and re-added the project from the solution.
Updated VS.
Repaired VS.
Deleted the app.config file and the variables from the Project Properties - System dialog, then added a new app.config and recreated the variables in the System dialog.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="system.diagnostics" type="System.Diagnostics.DiagnosticsConfigurationHandler"/>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" >
<section name="Doghunter.My.MySettings" type="System.Configuration.ClientSettingsSection, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" requirePermission="false" />
</sectionGroup>
</configSections>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="Doghunter"/>
</sharedListeners>
</system.diagnostics>
<applicationSettings>
<Doghunter.My.MySettings>
<setting name="TestKey" serializeAs="String">
<value>Henry</value>
</setting>
<setting name="NewKey" serializeAs="String">
<value>Moscow</value>
</setting>
</Doghunter.My.MySettings>
</applicationSettings>
</configuration>
Rebuilding the project as a Windows Forms Application (.Net Framework 4.7.2) resolved it.
Originally chose 'plain' Windows Forms Application, which used 5.0
No idea why this is the case, but now works so all good.

Could not load file or assembly 'System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

Using Visual Studio 2015 & Sitecore 8.0
I have created a new MVC project with .Net Framework 4.6.1, copied, and referenced the Sitecore.kernel.dll & Sitecore.mvc.dll. Replaced the existing web.config & global.asax with the sitecore files.
Added a plain HTML page in the solution and tried to browse it (from VS). The page opens with this error
Could not load file or assembly 'System.Web.Http, Version=5.1.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The system cannot find the file specified.
This HTML page is not published to Sitecore yet. I'm guessing, the Sitecore.mvc.dll wants MVC 5.1.0 and the MVC in VS project is 4.0.
So, I tried the following steps.
Run install package command in package manager console for MVC 5.1.1 which shows the error
Unable to load the service index for source
https://api.nuget.org/v3/index.json.
Deleted the %AppData%\NuGet\nuget.config and restarted VS2015
Manually browsed https://api.nuget.org/v3/index.json, but the page doesn't load - Secure connection failed. But this - http://api.nuget.org/v3/index.json will promt to download the json. So, in the nuget changed https to http, but its the same error - Unable to load the service index...
I'm not even able to start my first Sitecore MVC project when this happens.
I replaced the sitecore web.config with the regular VS project's web.config and now I'm able to browse the page. I set its Copy To Output Directory to Do not copy, so that it will not get published.
Will I be able to proceed with my Sitecore development this way without any major issues in future.
You need to do a few things to fix this:
Your VS project should be updated to have the web.config file from the Sitecore installation. You must have the Sitecore version of that file or Sitecore will not run correctly
You should add a binding redirect for the MVC file that makes everything use the latest version:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
etc....
</runtime>
Make sure that the web.config file in your /views folder contains the correct versions of all the MVC assemblies. This is mine for a .net 4.6 application:
<configSections>
<sectionGroup name="system.web.webPages.razor"
type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host"
type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" />
<section name="pages"
type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host
factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.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="Sitecore.Mvc" />
<add namespace="Sitecore" />
</namespaces>
</pages>
</system.web.webPages.razor>
Once that is done, your Sitecore installation should pick up the latest MVC binaries correctly.

VS 2015 IntelliSense: Assembly Not Referenced Error

I just switched to VS 2015. I have an older MVC 5 app that runs against 4.52. In VS 2013 it's perfectly fine.
In VS 2015 I'm getting red squigglies under my #Html.TextBoxFor() with an error indicating:
The type 'Expression<>' is defined in an assembly that is not
referenced. You must add a reference to assembly 'System.Core,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
The project builds and runs just fine -- but I am concerned about the IntelliSense error that never happened in VS 2013. Okay, so I try to add the reference to System.Core as recommended in the error above and then I get this error:
A reference to 'System.Core' could not be added. This component is
already automatically referenced by the build system.
Again, this is fine in VS 2013.
I had the same issue, but in the mean time I've found the answer:
I had to add the following references to my web.config (add inside the opening system.web tag):
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=3.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.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
I also changed the target framework from 4.5.1 to 4.5.
p.s Close and reopen Visual Studio after changing it.
I have tried most of these, what eventually worked for me was unloading the project, edit the csproj file, and add the following:
<Reference Include="System.Core" />
In my case (VS 2019) I have to add this in .csproj file after unload the project
<Reference Include="System.Core" />
need to change in ProjectGuid tag like below:
<ProjectGuid>{6C651A5E-8DDA-4680-804E-F9596743CBE8}</ProjectGuid>
then reload the project and add this tag to web.config like below:
<compilation debug="true" ...>
<assemblies>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
...
</assemblies>
</compilation>
and after that just rebuild the project and all the error is gone in my case.
Only deleting solution and getting solution from source control solved this for me, removing .vs folder and starting VS2015 as "devenv.exe /resetuserdata" did not solve my problem, event removing MEF component cache did not solve as per Razor intellisense not working in VS 2015 answers.
In my case, it worked after changing the tag <ProjectGuid> in .csproj file to <ProjectGuid>{6C651A5E-8DDA-4680-804E-F9596743CBE8}</ProjectGuid> and reopening the solution. All of the solutions posted above did not work for me.
From updating from 4.5.2 to 4.6.1 I got these exact errors in my views. Building and running the solution worked absolutely fine. After trying all the solutions already posted here, (and also checking intellisense for working, clearing caches, removing bin and obj folders, loading and reloading the project) nothing worked whatsoever (system.core was already being built correctly and adding in those references to the Web.config did nothing). I did my own digging and eventually found that in the project where the error was occurring the Web.config file contained two compilation debug target frameworks and a different httpRuntime target framework. Like so:
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.6.1" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.1" />
...
The solution was to resolve this by removing the extra compilation debug target framework and to ensure all target frameworks were the one I wanted (4.6.1)
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
...
Double check this if nothing else works. Hope that helps someone!
If anybody is facing this issue with
VS 2017 , .net framework version 4.8 and MVC version 5.2.7
Then check your Microsoft.CodeDom.Providers.DotNetCompilerPlatform version, If you have 2.0.1 version installed then downgrade it to 2.0.0
Then check the MVC version, you have to downgrade it to 5.2.4
Then downgrade Microsoft.AspNet.WebPages 3.2.7 to Microsoft.AspNet.WebPages 3.2.4
and Microsoft.AspNet.Razor 3.2.7 to Microsoft.AspNet.Razor 3.2.4
try to run the application now, it will work.
I tried these and other solutions on other Stack Overflow threads. None worked.
What worked was repairing the installation of Visual Studio which is found in the System Settings, Apps & features sub-menu (click on VS and choose "Repair"). It took a couple of hours, but then the problem disappeared.

Sitecore Intellisense broken under website

I installed a sitecore instance with SIM, and checked the project for a MVC solution, what I don't get is why the #Html.Sitecore() is not working:
Error 2 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Sitecore' and no extension method 'Sitecore' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
I have the /Views/Web.config with the following:
<namespaces>
<add namespace="Sitecore.Mvc" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
Is there something different that needs to be done when running a solution inside the "Website" folder vs publishing to the Website folder?

Add namespace to all views in ASP.NET MVC 6

I’m using MVC 6 and would like to be able to access a particular namespace globally from all of my Razor views. In MVC 5 this was fairly simple; I’d just add the following code to my ~/views/web.config file:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.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="MyProject.WebUI" />
<add namespace="MyProject.WebUI.Helpers" /><!-- Added this line -->
</namespaces>
</pages>
</system.web.webPages.razor>
Where I’ve added access to the MyProject.WebUI.Helpers namespace.
In ASP.NET 5, and therefore MVC 6, the web.config file has be done away with, so I’m not sure how to go about doing this any more. I’ve tried searching for an answer, but all I can find is how to do it in current versions of ASP.NET rather than v5.
Any ideas?
Edit: Clarified which web.config file I would have used.
For <= beta3 bits (what you're most likely using) you should add an #using statements to your _ViewStart.cshtml. Aka:
_ViewStart.cshtml: #using MyProject.WebUI.Helpers
If you don't have a _ViewStart.cshtml you can create one and just make sure it's in the same path or parent path of the view you want it to affect.
For beta4 bits, this functionality was moved to a new file called _GlobalImport.cshtml; _ViewStart.cshtml was transitioned back to its original functionality (just running code, not inheriting directives). Therefore:
_GlobalImport.cshtml: #using MyProject.WebUI.Helpers
For beta5 bits, _GlobalImport.cshtml was renamed to _ViewImports.cshtml
Add your namespaces to the_ViewImports.cshtml file (it's under the Views folder).
Example file:
#using Microsoft.AspNetCore.Identity
#using Jifiti.Registry.Web.Models.AccountViewModels
#using Jifiti.Registry.Web.Models.ManageViewModels
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
I'm on beta 7 and I had to use
#using System.Security.Principal
#using System.Security.Claims
#Context.User.GetUserId()