The type 'Expression<>' is defined in an assembly that is not referenced
Asked Answered
D

32

100

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.

Dissuasion answered 27/6, 2015 at 8:43 Comment(1)
Possible duplicate of VS 2015 IntelliSense: Assembly Not Referenced ErrorImmobilize
B
45

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.

  1. Go to Project-> NuGet Package Manager.
  2. Search for MVC; it's going to be the first result.
  3. 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.

Britzka answered 6/1, 2016 at 21:54 Comment(5)
How did you notice your project required 5.2.0?Scandalmonger
@Scandalmonger I think it depends on .NET Framework version: .NET 4.5.2 -> MVC 4.0.0; .NET 4.6.0 -> MVC 5.2.0; .NET 4.6.1 -> MVC 5.2.3. (Not confirmed)Aker
I'm using .net 4.7 and still have this annoying errorPyrolysis
I had the same issue, turns out it was my debug value in the web.config was set to false. Set it to "True" <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" />Silkstocking
I have tried the above solution its working well. My VS Solution Version was of 4.6.2 and I downgraded the MVC to 5.2.3 from 5.2.4 .So I came to know that MVC 5.2.3 is compatible with .Net 4.6.2Virago
S
68

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://mcmap.net/q/212596/-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..

Sequester answered 3/8, 2015 at 13:59 Comment(5)
already i said that.. i have try this one before i posting this question. any way thanks both of youDissuasion
fine! It works. Note that you should close and reopen cshtml file to take effect.Retouch
This worked for me once I went through all the web.configs and made sure the versions matched as in https://mcmap.net/q/211182/-the-type-39-expression-lt-gt-39-is-defined-in-an-assembly-that-is-not-referencedCongratulatory
It works, but i found this solution a little bit dirty. Anyway, in my case i add only the reference to System.CoreSelfreliant
This solution isn't "dirty". It's literally exactly what the error tells you to do. What I didn't understand was that it needed to go into the web.config file.Willem
X
54

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.

  1. 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:

Project Properties

  1. 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:

MVC version

Xenomorphic answered 23/5, 2016 at 1:7 Comment(6)
This Worked for me. Many Thanks.Mcreynolds
This resolved it for me too, but not directly. I had to delete the file with wrong version from my bin folder, update mvc via nuget, and then it placed the correct version in my bin folder and it worked. if you still keep getting the wrong version in the bin folder after updating via nuget, delete the existing one in the bin folderKulda
Thank you. I updated Microsoft.AspNet.Mvc through NuGet to v5.2.7 and began to see this error in .schtml file, while app was still working. It turned out web.config in Views folder under <system.web.webPages.razor> it was still v5.2.3. Modifying it to v5.2.7 helped.Kiethkiev
This worked - thank you! Remembering to check the [Views]/web.config was key for me. Version matched everywhere else but there!Kindhearted
grateful , in my case mvc version and mvc assembly version was different. i change the all 5.0.0.0 version to 5.2.3.0 in view/Web.config file.Cecilececiley
I think this should be the accepted answer !! solved my problem as well.Camphor
B
45

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.

  1. Go to Project-> NuGet Package Manager.
  2. Search for MVC; it's going to be the first result.
  3. 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.

Britzka answered 6/1, 2016 at 21:54 Comment(5)
How did you notice your project required 5.2.0?Scandalmonger
@Scandalmonger I think it depends on .NET Framework version: .NET 4.5.2 -> MVC 4.0.0; .NET 4.6.0 -> MVC 5.2.0; .NET 4.6.1 -> MVC 5.2.3. (Not confirmed)Aker
I'm using .net 4.7 and still have this annoying errorPyrolysis
I had the same issue, turns out it was my debug value in the web.config was set to false. Set it to "True" <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" />Silkstocking
I have tried the above solution its working well. My VS Solution Version was of 4.6.2 and I downgraded the MVC to 5.2.3 from 5.2.4 .So I came to know that MVC 5.2.3 is compatible with .Net 4.6.2Virago
N
27
  • Close the cshtml file
  • Rebuild solution
  • Open cshtml file
  • Still errors? Restart Visual studio
  • Still errors? Use ctm1988's answer
Niccolite answered 26/12, 2015 at 13:29 Comment(2)
I have tried everything was mentioned here... but still its not working.. while reinstalling visual studio its works perfectly.. i thought missing dependencies is the reason..Dissuasion
if you've had a project that was working and fine and suddenly you got this issue then this is probably the fix!Judsen
S
22

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://mcmap.net/q/212595/-vs-2015-intellisense-assembly-not-referenced-error

Sheronsherourd answered 31/10, 2016 at 8:56 Comment(1)
it works! looks like that visual studion thinks that System.Core has already been added but there really is not. So you need to do this manuallyProximo
D
12

Deleting [projectName].csproj.user file from the project directory helped for me.

Downing answered 1/11, 2018 at 11:56 Comment(3)
Worked for me Vs 2017 Asp MVC5Spectrophotometer
This one worked, VS 2019 & MVC5. I also cleaned out the bin/obj and .vs folders too.Car
This solved my problem. thank you.Hawthorne
R
7

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.

Rimma answered 5/4, 2016 at 3:14 Comment(4)
please add tag with any technology or more .. No tags FoundJoellenjoelly
Adding the compilation/targetFramework attribute worked for me.Xenomorphic
I had accidently added two different values for target framework, this solved my issue.Bouilli
This fixed the problem for me, a hybrid asp.net/webforms app: I upgraded the target framework of my application to 4.7, did not notice these two web.config settings still pointing to 4.5.Whitlow
T
6

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

Teens answered 12/12, 2017 at 15:14 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewExpropriate
@SteveG. Thanks, Steve! I've edited it. Haven't commented much before and lack some SO etiquette... :-)Teens
This one solved my problem , I have to mention that a Visual Studio Restart also was needed, Thank You.Unquiet
A
4

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.

Averyaveryl answered 3/8, 2016 at 8:17 Comment(1)
In my case, just close and restart VS. I have resharper but didn't need to do anything with it.Cerotype
T
4

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>
Trafalgar answered 14/5, 2020 at 10:42 Comment(0)
D
3

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">
Daze answered 13/3, 2018 at 11:20 Comment(1)
Thank you, @juFo! This fixed it for me in Visual Studio 2019 with MVC 5. My MVC project's target framework was 4.7.2 while the web.config still had 4.7.1 for the targetFramework in the <httpRuntime> and <compilation> tags. Simply updating the attribute to correctly reflect the project's .NET Framework version fixed it for me.Planimetry
B
3

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>
Brezin answered 23/7, 2018 at 12:8 Comment(0)
P
3

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

Palatalized answered 14/2, 2019 at 16:46 Comment(0)
U
2

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.

Unwilling answered 29/11, 2015 at 18:59 Comment(0)
T
2

Well, I got it fixed by correcting the assembly reference in /Views/web.config, see the image:

enter image description here

Trine answered 23/2, 2020 at 9:47 Comment(0)
E
2

delete bin, .vs and obj directory. after build your project on the visual studio.

Employer answered 6/4, 2020 at 10:3 Comment(1)
I do a lot of switching between versions of my project, a few that target different frameworks. This took care of my "System.Core" reference error. Thanks for the simple reminder that it is sometimes necessary to to clean up the VS environment between checkouts.Eskisehir
I
1

Restarting Visual studio worked for me.

Identity answered 28/1, 2016 at 0:25 Comment(2)
This happened to me after some seemingly innocent NuGet updates. It was just libraries like jQuery and bootstrap. After the updates I got this error message and restarting Visual Studio fixed it.Andrew
I just did this and it worked, go figure.Stutman
B
1

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.

Burmaburman answered 18/6, 2016 at 20:24 Comment(0)
B
1

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!

Bohr answered 18/2, 2018 at 8:42 Comment(1)
I had 12 packages that needed to be upgraded and the sln ran without any problems once I updated everything. Thank you.Malignant
L
0

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
Ldopa answered 3/1, 2016 at 14:13 Comment(0)
S
0

In my case the solution was to move the ConnectionString inside the Web.Config. This statement should be after AppSettings section.

Slaver answered 5/5, 2016 at 21:5 Comment(0)
B
0

I had this problem too. Changing targetFramework="4.5.2" to targetFramwork="4.5" in web.config file solved it.

Basidium answered 22/8, 2016 at 3:59 Comment(0)
S
0
  • Close the file
  • Clean the solution
  • Build the solution
  • Reopen the file
Scrophulariaceous answered 29/11, 2016 at 23:47 Comment(0)
N
0

Try to add

using system.Linq.Expressions;

Nichol answered 5/1, 2017 at 19:35 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Ultramarine
M
0

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?

Muldrow answered 24/1, 2017 at 20:58 Comment(0)
P
0

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.

Progressive answered 30/3, 2017 at 20:52 Comment(0)
A
0

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!

Ambulance answered 7/5, 2017 at 11:0 Comment(0)
I
0

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.

Inanity answered 27/10, 2017 at 5:22 Comment(0)
N
0

Installing the System.Core package with NuGet package manager works for me

Northumberland answered 14/10, 2022 at 9:21 Comment(0)
C
0

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.

visual studio installer

After installing that targeting package, my issue was resolved.

Chopine answered 1/12, 2022 at 15:48 Comment(0)
F
0

This worked for me in VS 2022:

  • Unload project
  • Reload project
  • Rebuild project/solution
Flibbertigibbet answered 31/3, 2023 at 4:24 Comment(0)
T
0

I had to :

  • restore the web.config and packages.config files to a state from github where everything was fine.
  • delete this line from packages.config and delete System.Web.Mvc.dll from the references

<package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net48" />

  • run this command to install (System.Web.Mvc.dll) (Microsoft.AspNet.Mvc) again and set the version according to the version mentioned in web.config

    Install-Package Microsoft.AspNet.Mvc -Version 5.3.0.0

https://www.nuget.org/packages/Microsoft.AspNet.Mvc/5.2.9

Trussing answered 28/2 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.