问题
I'm working on an MVC 4 project started in Visual Studio 2010. Right now I'm working on a machine with Visual Studio 2012 as I don't have access to the machine I was originally working on. I tried all morning to find answers, but they don't seem to help my situation.
I followed How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app and installed from nuget right into my solution. Even though I have all the required reference packages downloaded and installed on my machine, System.Web.Optimization
continues to remain missing. Is there anything else I can do?
Edit: When I try to build the project I receive the following errors
- The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) - BundleConfig.cs
- The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) - Global.asax.cs
- The type or namespace 'BundleCollection' could not be found (are you missing a using directive or an assembly reference?) - BundleConfig.cs
My BundleConfig.cs contains
using System.Web;
using System.Web.Optimization;
namespace BCCDC_AdminTool
{
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/? LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
}
}
}
回答1:
This is a long shot but here it goes anyway, right click on the project --> Property Pages and make sure System.Web.Optimization entry exists. If not, copy the System.Web.Optimization.dll to your bin folder or import it as a reference.
Edit: mvc projects don't have the "Property pages" menu option.
One other thing you can try is create a new mvc4 basic project, go to the bin folder of the solution and copy the System.Web.Optimization.dll to the bin folder of your other project that is giving you the error. If you can't find the dll then try updating the nuget packages.
回答2:
I had the same problem with a VS2010 project I opened in VS2012. The Microsoft.AspNet.Web.Optimization package was missing so I just needed to download it using Nuget Package Manager.
You can run in the console: Install-Package Microsoft.AspNet.Web.Optimization
回答3:
I know it's confusing, but you will need to Install-Package Microsoft.AspNet.Web.Optimization
回答4:
Try going to the web.config in the Views folder and removing the following line:
<add namespace="System.Web.Optimization" />
That fixed it for me.
回答5:
Go to tools >> Library Package Manager >> Manage Nuget Packages >> install Microsoft ASP.NET web optimisation framework
回答6:
I battled this for an hour and none of the above suggestions worked. In the end only the following worked (for some obtuse reason) :
- ctrl-q -> type "package manager console"
Uninstall-Package Microsoft.AspNet.Web.Optimization <problem project name>
Install-Package Microsoft.AspNet.Web.Optimization <problem project name>
WARNING/HEADS-UP
- the above will install the latest version of
Microsoft.AspNet.Web.Optimization
. If you want a particular version (say v1.1.1), useInstall-Package Microsoft.AspNet.Web.Optimization <problem project name> -Version 1.1.1
- don't forget the
<problem project name>
parameter or nuget will operate at the Solution-level, possibly trampling sibling projects. - Note that omitting the project name is an of itself really handy for doing Solution-level version updates or package installs.
回答7:
Uninstalling, and reinstalling the Microsoft.AspNet.Web.Optimization package solved it for me.
回答8:
Reinstalling system.web.optimizaiton using Nuget package manager solved this issue for me..
回答9:
I fixed this issue by setting the reference to the assembly System.Web.Optimization.dll to copy local to true.
回答10:
What worked for me in a specific scenario (I downloaded a MVC project from source control and received that error), was to open a new MVC project on the same solution. Afterwards I could delete the new project I've created and it still works (on VS2012).
回答11:
I know there are many answers to this issue, but give this a try- open the project in visual studio as an Administrator.It did solve in my case.
来源:https://stackoverflow.com/questions/16179293/system-web-optimization-and-microsoft-web-optimization-wont-load-reference-in-v