Related
We've been trying to pre-compile the views for our application to improve the render times on initial form display.
We're using Visual Studio 2017, MVC 5, MS Membership for security logins.
When Publishing to the web application site without 'Precompile during publishing' set it all builds and the application runs as expected with a login form shown.
When the 'Precompile during publishing' is set on then it builds ok.
But when starting the application we get a 'HTTP Error 404.0 - Not Found' error
and no login screen shown !
I've checked the folders' security, location paths permissions allow All Users.
When publishing with Precompile set on I get the following warnings;
2>ASPNETCOMPILER(0,0): Warning : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Microsoft.ReportViewer.WebDesign, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91. The dependencies are: System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly.
2>ASPNETCOMPILER(0,0): Warning : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Microsoft.ReportViewer.Design, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91. The dependencies are: System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly.
2>ASPNETCOMPILER(0,0): Warning : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: Microsoft.Build.Tasks.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. The dependencies are: System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a; System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly.
I've tried to get rid of these warnings by adding assembly lines into the web.config's section but to no effect.
Best guess without seeing any of your .config or .csproj files is that you are missing the following section from your Web.config file:
<system.web>
<compilation>
<assemblies>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<system.web>
This only works if the referenced DLL is in the GAC. If it is not:
Create a /Bin folder in your website root.
Copy your DLL there.
Do not add a reference in web.config.
References:
Adding System.windows.Forms reference into asp.net website
ASP.NET Compiler complaining of mismatching framework versions with MiniProfiler
If That Doesn't Work
Visual Studio is a great tool. But it isn't so great at keeping dependencies in sync across a solution with multiple projects or even between project files and config files. Usually, when you need to upgrade or downgrade a dependency, something will inevitably end up being inconsistent between the main project's .csproj file and one or more dependent assembly .csproj files, or it may even be out of sync with .config files.
The only 100% reliable way to get past a scenario such as this is to manually review each dependency and ensure the version is consistent throughout all projects and config files.
Visual Studio 2017
Fortunately, in VS 2017 they made this easier to do. You can now simply right click the project and select Edit <projectName>.csproj.
Prior Versions of Visual Studio
Right-click on your project node in Solution Explorer and click Unload Project.
Right-click the project node again and click Edit <projectName>.csproj.
What to Look For
Here is an example of a MVC version mismatch that was resolved this way. It might help to create a fresh project from the MVC 5 template to see what an updated project is supposed to look like and then compare the differences between Web.config, Views/Web.config and .csproj files, and then cycle through each of the rest of the dependencies ensuring the version numbers are consistent and up-to-date.
Make sure to check if the .csproj files are using MSBuild conditions, as Visual Studio has no way to update these and they tend to be a major source of problems when it comes to upgrading.
Edit
In IIS I set up a Failed Request Trace and the first item created contained; ModuleName UrlAuthorization Notification AUTHORIZE_REQUEST HttpStatus 401 HttpReason Unauthorized etc...
The message indicates that your application is setup to use UrlAuthorization, and upon further research it appears it could either be configured as IIS URL Authorization or ASP.NET URL Authorization
If using ASP.NET URL Authorization, you will have a web.config entry like
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" preCondition="managedHandler" />
If using IIS URL Authorization, you will have a web.config entry like
<add name="UrlAuthorizationModule" image="%windir%\System32\inetsrv\urlauthz.dll" />
I created a new MVC 5 project from the VS 2017 template (w/individual user accounts) and neither of these is typical of an MVC 5 application. I am not going to tell you that removing these is the solution, because there might be some valid reason why your application is using URL Authorization. It is apparently still the best way that an application can lock down files so they can't be served without logging in. Although, if it can be proven this is the root cause by removing the module you are using, then it will just be a matter of working out how to configure the URL Authorization module to work with MvcBuildViews enabled.
Cutting out the Membership code and re-publishing didn't make any difference.
So I created a new MVC Project and roughly migrating the application's code into it. And it still publishes and runs compiled views !
So I know how to fix it, still don't know why but software's sometimes like that.
Thanks to all who tried to help esp. NightOwl888.
Our project (.Net Framework 4.6.2) was chugging along just fine on EF Core 1.1.0. Then we decided to upgrade to EF Core 2.0.0. Went through a few days of tweaking the code to use new syntax, getting our unit and integration tests to pass again, and finally they did. So today we deployed onto our QA site, which immediately broke with a DLL Hell message:
Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
During the course of the upgrade I had updated System.Net.Http from 4.3.0 to 4.3.3, so I tried reversing that. Didn't help. I tried commenting out the dependent assembly reference in web.config, and that made the message change:
Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Hello! If I've understood correctly, that means that version 4.1.1.0 of System.Net.Http (which is the correct one) is actually being found by the binding redirect - but there's some other assembly that's coming along with my deployment, which is looking for version 4.0.0.0, and ignoring the binding redirect.
How do I find which assembly is messing me up? And how do I get it to respect the binding redirect?
Apparently I'm not the only person to have been sucker-punched by this issue. See here, here and here.
TLDR: Visual Studio 2017 comes packed with a bunch of the latest and greatest DLLs, which are so very good, that MSBuild will impose them on you if you're working with .NET Framework 4.6.2 or earlier, because why would you want to use those cobwebby old versions that you specified in your binding redirects?
I've tried two solutions that worked:
Quick & dirty: Go into your Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461 folder. You'll find lib and ref folders there. Go into each of these and create a subfolder called HideMe or suchlike. Then move all the DLLs except netstandard.dll into the HideMe folder. Now MSBuild can't impose its ideas of which DLL is best for you.
The proper way: Upgrade to .NET Framework 4.7.1. That will be a little more labor-intensive and might break stuff, but at least you don't have to do hacks like my first workaround.
Since updating to Visual Studio 2015 Update 1, my MVC 4 project has compile errors in several view files. The errors are related to the following:
For loops (you must declare a type for the temporary variable or it is not recognized)
Linq expressions are not recongized (For example: Orderby is not a member of List(Of String))
Html.DisplayFor is not recognized (Reference required to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' containing the type 'Expression(Of )'. Add one to your project.)
Other than these 3 type of errors, the intellisense in vbhtml files seems fine. And in VS2013 none of the above was an issue so these errors did not exist before updating.
According to this post, it sounds like my version of Visual Studio should have this resolved. I have tried adding a reference to System.Core but I get an error that it has already added. I also tried deleting the 14.0 folder from the AppData folder but that didn't help. Any suggestions?
I had the same issue in Visual Studio 2017. I was able to fix it by updating the version numbers for System.Web.Mvc in the <pages> section of Views\web.config to match the version referenced by the project. Thanks to JamieD77's comment for pointing in the right direction.
In ASP.NET MVC 4.5.2 Framework, after typing #Html.LabelFor() or #Html.EditorFor() in a view, I'm getting this error:
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'.
I have added assembly reference System.Core.dll, Version 4.0.0.0, Runtime Version v4.0.30319, and also I did in web.config.
I am not sure if you are still having this issue or not but i was having the same issue as well.
I was able to find the solutions here
https://stackoverflow.com/questions/6496223/compilation-error-in-net-4-0-web-config-linq-not-found
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
I hope this helps..
This error means that Visual Studio is unable to locate the System.Web.Mvc assembly to power its intellisense for Razor views. One or both of the following may be required to fix it.
Ensure the version of the .NET framework for the compilation
property in the main web.config (the one in the website root) is the
same as that specified in the project properties.
[root]/Web.config:
<system.web>
<compilation targetFramework="4.6" />
Project Properties:
Ensure the version of the MVC assembly specified in the views
web.config (the one in the views folder) is the same as the MVC
assembly you are using in your project.
[views folder]/web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
MVC Assembly Reference Properties:
I have run in the same issue as you, albeit much later. The issue was that I was not able to access Razor views (.cshtml) as I was getting an error stating that I had a missing assembly reference, namely System.Web.Mvc, even though it was in the project references. After investigation, I have noticed that the installed NuGet version was 5.2.3, while the project required 5.2.0. The solution is to downgrade the MVC version.
Go to Project-> NuGet Package Manager.
Search for MVC; it's going to be the first result.
Next is to select downgrade from the drop-down in the details of the
NuGet package and submit.
Confirm all the dialogs and you are ready to go.
Close the cshtml file
Rebuild solution
Open cshtml file
Still errors? Restart Visual studio
Still errors? Use ctm1988's answer
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" />
https://stackoverflow.com/a/38371981/2335660
Deleting [projectName].csproj.user file from the project directory helped for me.
I just had the same exact issue and none of the solutions fixed the problem. I had to add this into my web.config within System.Web
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
I had removed it when changing some of the config around.
I've had the same problem with missing assembly and it happened that VS2017 build has not copied it properly into the Bin folder. These steps helped me:
Navigate to the web project's References node
Find the reference to System.Core
Open the VS Properties Window
In the properties window, change Copy Local: False to True
from this comment:
CS0012: The type '#####Any type#####' is defined in an assembly that is not referenced
In my case this message was shown only when Resharper was turned on. I have cleared Resharper cache, restarted VS, turned Resharper off and turned it on again. The message has dissapeared.
I did ALL of the above and in the end found that what solves it for me (on a .net framework 4.7.1 site with MVC 5.2.4) was adding this into the root web.config within <compilation> -
<assemblies>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
In Web.config I needed to change:
<system.web>
<compilation debug="true" targetFramework="4.7">
to
<system.web>
<httpRuntime targetFramework="4.7" />
<compilation debug="true" targetFramework="4.7">
Check that the cshtml file Build Action is set to 'Content'.
I use ReSharper and have noticed that for some reason the file that is generated defaults to 'None' when invoking the template.
Add the following to Web.config (in root). I tried absolutely everything of earlier suggestions and nothing worked until I found the below. Hope it will save time for someone else.
I use targetFramework="4.6.1", but change it to the version you use if different.
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
Add the System.Core.dll file to the bin folder manually. Restart VS and build project.
Solved for me
Had design errors in all views on #Html helpers and on my Kendo grids.
Adding System.Core assembly in Web.config did nothing, nor did downgrading the MVC version. And error when trying to add reference to System.Core manually:
A reference to 'System.Core' could not be added. This component is
already automatically referenced by the build system.
Make sure to check that TargetFramework in project properties matches the one in project web.config.
Screenshot of bin folder
Well, I got it fixed by correcting the assembly reference in /Views/web.config, see the image:
delete bin, .vs and obj directory. after build your project on the visual studio.
Restarting Visual studio worked for me.
For me, there was an option under the project menu to "Enable C# 6.0/VB 14". Selecting this option did the trick. Previously I tried modifying the web.config, restarting Visual Studio, etc as was suggested in this post.
I faced the same problem. Basically the problem is using different version of System.Web.Mvc. So the best thing you should the open Tools > Nuget Package Manager > Manage Nuget Packages for Solution. You can probably see System.Web.Mvc on the Cosolidate tab. Upgrade or downgrade would be the best option. If you use the latest .Net framework, which is currently .Net 4.6.1, you should upgrade the all System.Web.Mvc versions to 5.2.4.0. Good luck!
This happened with one of my views - all the others were fine. I did the following:
Create a new view
Copy and paste the contents from the old view to the new one
Deleted the old one
Renamed the new one to the old one
Close the file
Clean the solution
Build the solution
Reopen the file
Try to add
using system.Linq.Expressions;
I had this problem too.
Changing targetFramework="4.5.2" to targetFramwork="4.5"
in web.config file solved it.
If you get this in only one or two CSHTML files... it's a corrupt file.
I've had this happen with a corrupt file that has some hidden unicode characters.
Try copying all the text of the document to a fresh notepad instance.
Clear the contents of the original file and save... if the file length is > 0, there's something awry.
Delete the file and save your notepad instance over the old version.
All the errors went away for me.
I've had this happen a couple of times in my solution. Not sure what's causing it, but I suspect it's Resharper screwing up when it starts to swallow huge chunks of memory into the abyss.
Am I the only one who has a love / hate relationship with Resharper?
This can be caused if you have multiple projects in your solution that don't all target the same version of the .NET Framework. For example, if your web app project targets .NET 4.5 and you have an assembly reference to another project in the solution that targets .NET 4.5.2. To fix this you need to change your projects to all target the same version of the .NET Framework.
If none of the answers work use this one. I faced the same issue! Actually configuration.cs file in migrations folder was missing/deleted. I did delete migrations folder and also did delete migrations table from database. Next in Package Manager Console (PM) used following commands:
Enable-Migrations -Force
Add-Migration Initial
update-database
This process recreated the configuration.cs file and everything worked perfectly!
Check your project properties to get the Target framework. I resolved this issue in my project by matching for my target framework .NET framework 4.6.1 MVC 5.2.3 was the apt solution. I got that MVC version from NuGet.
In my case the solution was to move the ConnectionString inside the Web.Config. This statement should be after AppSettings section.
Installing the System.Core package with NuGet package manager works for me
In my case I checked the targetFramework of Microsoft.AspNet.Mvc in the packages.config file.
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net462" />
And I found that the targeting package was not installed.
After installing that targeting package, my issue was resolved.
I created a new ASP.NET MVC 5 web project in VS 2013 (Update 1) then updated all NuGet packages. When I build the project, I get the following warning:
warning MSB3243: No way to resolve conflict between "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" and "Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed".
When I check the web.config, however, I see that a binding redirect is in place:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
Which is exactly what the warning advises.
How can I fix this warning?
Here the steps I used to fix the warning:
Unload project in VS
Edit .csproj file
Search for all references to Newtonsoft.Json assembly
Found two, one to v6 and one to v5
Replace the reference to v5 with v6
Reload project
Build and notice assembly reference failure
View References and see that there are now two to Newtonsoft.Json. Remove the one that's failing to resolve.
Rebuild - no warnings
I had this problem because I updated packages, which included Microsoft.AspNet.WebApi that has a reference to Newtonsoft.Json 4.5.6 and I already had version 6 installed. It wasn't clever enough to use the version 6.
To resolve it, after the WebApi update I opened the Tools > NuGet Package Manager > Pacakge Manager Console and ran:
Update-Package Newtonsoft.Json
The log showed that the 6.0.x and 4.5.6 versions were all updated to the latest one and everything was fine.
I have a feeling this will come up again.
I found to delete this section from the project file fix the problem.
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
I upgraded from Newtonsoft.Json 11.0.1 to 12.0.2. Opening the project file in Notepad++ I discovered both
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
and
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
I deleted the ItemGroup wrapping the reference with the hint path to version 11.0.1.
These issues can be insanely frustrating to find. What's more, developers often follow the same steps as previous project setups. The prior setups didn't encounter the issue. For whatever reason the project file occasionally is updated incorrectly.
I desperately wish Microsoft would fix these visual studio DLL hell issues from popping up. It happens far too often and causing progress to screech to a halt until it is fixed, often by trial and error.
If none of the above works, try using this in web.config or app.config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
The final solution to your assembly redirect errors
Okay, hopefully this should help resolve any (sane) assembly reference discrepancies ...
Check the error.
Check web.config after the assembly redirect. Create one if not exists.
Right-click the reference for the assembly and choose Properties.
Check the Version (not Runtime version) in the Properties table. Copy that.
Paste into the newVersion attribute.
For convenience, change the last part of the oldVersion to something high, round and imaginary.
Rejoice.
Remember that with the binding redirection
oldVersion="0.0.0.0-6.0.0.0"
You are saying that the old versions of the dll are between version 0.0.0.0 and version 6.0.0.0.
No one mentioned the following, which in my understanding is the correct solution:
Go the csproj of the project where the nuget is installed, and set the AutoGEneratedBindingRedirects to false.
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
Full article in MSDN.
I updated my package and even reinstalled it - but I was still getting the exact same error as the OP mentioned. I manually edited the referenced dll by doing the following.
I removed the newtonsoft.json.dll from my reference, then manually deleted the .dll from the bin directoy. Then i manually copied the newtonsoft.json.dll from the nuget package folder into the project bin, then added the reference by browsing to the .dll file.
Now my project builds again.
I had similar issue and just wanted to post an answer for others in my situation.
I have a solution running a ASP.NET Web Application with multiple other C# class lib projects.
My ASP.NET Web Application wasn't using json, but other projects where.
This is how I fixed it:
I made sure all projects where using latest version (6) using NuGet Update on all projects currently using any version of json - this didn't fix the issue
I added json to the web application using NuGet - this fixed the issue (let me dive into why):
Step 2 was first of all adding a configuration information for json, that suggest that all projects, use the latest version (6) no matter what version they have. Adding the assembly binding to Web.Config is most likely the fix.
However, step 2 also cleaned up som legacy code. It turned out we have previously used an old version (5) of json in our Web Application and the NuGet folders wasn't deleted when the reference was (I suspect: manually) removed. Adding the latest json (6), removed the old folders (json v5). This might be part of the fix as well.
Veverke mentioned that it is possible to disable generation of binding redirects by setting AutoGEneratedBindingRedirects to false. Not sure if it's a new thing since this question was posted, but there is an "Skip applying binding redirects" option in Tools/Options/Nuget Packet Manager, which can be toggled. By default it is off, meaning the redirects will be applied. However if you do this, you will have to manage any necessary binding redirects manually.