Visual Studio 2015 - MVC 4 - Razor compile errors - asp.net-mvc

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.

Related

How to resolve DLL Hell introduced since upgrading to EF Core 2.0?

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.

Problems after migrating VS2013 to VS2015 in MVC

UPDATED QUESTION
I have a project that I've started in VS 2013 and was setup as the following:
MyMainMvcApp (Containing core Functionality)
MyPlugin (Containing plugable customer stuff)
MyPlugin contains some *.cshtml views as embedded resource and some controllers.
In VS2013 I was able open the Views and i had full Razor Intellisense and no issues when compiling.
IN V2015 the Error List is showing a lot of errors like
Feature 'lambda expression' is not available in C# 2. Please use language version 3 or greater.
The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
etc etc
Of course I have all the Assemblies referenced. And the projects even compile.
I believe that VS 2015 is just missing some config stuff in web.config or similar, so that he can resolve the stuff while having the view open in designer.
I have an app.config in MyPlugin project that has been added from Nuget.
I've put the same config stuff as my web.config in there but that didn't help.
As well I copied the MyMainMvcApp\Views\web.config into MyPlugin\Views\web.config that didn't work as well.
Any idea what the issue might be or ideas how to solve it?
I've started MyPlugin as a Class Library in VS 2013 and that worked well. Afterwards I was not able to make MyPlugin work as desired in VS 2015 designer. I've created a new MyPlugin Project as a MVC Project and copied all my code from the old library to the new.
It's a workaround but that solves my VS 2015 problems.
The fact that it's trying to utilize C# 2, is pretty concerning. Make sure your project is targeting the latest version of .NET you feel comfortable with. Since you're using VS2015, go all the way to 4.6 if you like, or if you want something a little more hardened, stick with 4.5.2. Just right-click your project in the Solution Explorer and choose "Properties". Then on the first "Application" tab, change the "Target framework" drop-down to something appropriate.

Taghelper: Cannot resolve TagHelper containing assembly

I cannot get a custom TagHelper to work in my ASP.NET v5 project. The custom TagHelper is located in the web project itself. I tried following this sample this sample from CodeProject and this tutorial too.
When I try to use the tag without addTagHelper:
nothing is transformed, no breakpoints hit in the tag helper.
When I try to #addTagHelper "*, MyWebApplication" to the Viewcomponent, the error is MyWebApplication'. Error: Could not load file or assembly 'MyWebApplication'.
What am I missing?
Tool versions:
Microsoft Visual Studio Enterprise 2015 RC
Version 14.0.22823.1 D14REL
Microsoft .NET Framework Version 4.6.00057
Microsoft.AspNet.Mvc 6.0.0-beta4
The advertised signature for addTagHelper is
#addTagHelper "className/Filter", "assemblyName"
Behaviour actually appears to be (at least for helpers in same web project)
addTagHelper "className/Filter", "folderName"
because at least into Beta6, the assembly name is the folder name and ignores the attributes in the project properties.
Here is a sample working demo
Hit F5, view cats.
Edit the project properties to change the assembly name.
Hit F5, view cats. << This should not work?!
Close solution, rename the on disk directory TagHelperDemo ->
TagHelperFolderDemo
Observe page fails until the folder name is used for addTagHelper directive
PS: #addTagHelper is required even when the TagHelper is in the same project. VS2015 re-colours the tag as purple when it is targeted by a TagHelper.

How to remove System.Web.Optimization from MVC5

I have a new ASP.NET MVC5 project. I don't want to use Microsoft bundling and minification so I removed the nuget package and the dependencies that I also don't want.
I ensured there was nothing left in the config and I cleared out \Temp\Temporary ASP.NET Files\
I am using VS2013 / IIExpress and there is nothing that references System.Web.Optimization in the aspnet.config or the applicationhost.config
...the solution builds fine but when I run it throws:
Compiler Error Message: CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
Something somewhere is trying to pull in the dll, but it's not my code. Seems very odd!
Line 26: using System.Web.Optimization;
Surely it's not reference by MVC5 itself. Anyone seen this before?
I just found it in the Views folder when I did a search for 'namespaces', no idea how I missed it. I didn't even know there was a config file in the Views folder.
I found
<add namespace="System.Web.Optimization" />
in the Web.config file under the Views folder. It doesn't get removed with the package.
In Visual Studio 2012, There was a problem after creating a new Area. In Web.Config of that Area, There was a statement (line) that Visual Studio Compiler wants to use Microsoft.AspNet.Web.Optimization! And when you run the application and you want to see some of views in that Area, You get an error because of that statement (line).
Solution (1)
Delete that statement (line) Microsoft.AspNet.Web.Optimization in Web.Config of Areas (in Visual Studio 2012)
Solution (2)
You can install the Web.Optimization for your application with NUGET:
INSTALL-PACKAGE Microsoft.AspNet.Web.Optimization

ASP.NET MVC 5 Visual Studio 2013

i'm working on a project which is using mvc 5,ef 6.1, visual studio 2013. First i start coding from admin panel after its done i get back to admin panel for some changes.
Then i got those errors : http://prntscr.com/34tuby
ViewBag, Html Helper is not recognizing.
Project can compiling and working but when open a cshtml (view) file in visual studio i got those error.
for solution i tried;
Clean/Rebuild Project
Compare web.configs
Compare View web.configs
Compare project properties
Compare packages config
all packages same, web.config files same , all packages version same with working project.
-Thanks
Try checking your configuration file, as mentioned in this SO post. It's usually related to config entries not matching the correct DLL's.
I create new project and transfer all files with same namespace. And it work

Resources