38

I am deploying a new website as my main site and it works beautifully.

All of my applications under the root url work as well, except for one. It is a legacy system (c#.net) that is heavily used and unfortunately, I do not have access to the source code.

When I run the legacy application www.mysite.com/crm I get the following error:

Server Error in '/crm' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to 
service this request. Please review the following specific error details and modify 
your source code appropriately. 

Compiler Error Message: CS0234: The type or namespace name 'Optimization' does not 
exist in the namespace 'System.Web' (are you missing an assembly reference?)

Source Error:


Line 17:     <pages>
Line 18:       <namespaces>
Line 19:         <add namespace="System.Web.Optimization" />
Line 20:       </namespaces>
Line 21:     <controls>

Source File: e:\WebSites\newsite\Web.config    Line: 19 


Show Detailed Compiler Output:

Show Complete Compilation Source:


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446

The error is listing the web.config for the main site, which works. However, it happens only when I run the application under the main site.

This is running on Server 2008 R2 and IIS7

How do I correct this? What else can I try?

UPDATE

I don't know if it makes a difference, but the Web.Config it references in the error is for the new website, not for the legacy application. I am not sure why the main site config is a factor at all.

CLARIFICATION

I don't have access to the source code of the legacy application so I cannot adjust the properties for that application. Pasting the Optimization dll into the site didn't fix it either. It may work for other users, but not for me.

WORK AROUND

I setup the new site under another URL and redirect traffic to the root URL to the new URL and allow the legacy application to run as is until we can replace it. Very sloppy solution, but it works for now.

Brian
  • 5,826
  • 11
  • 60
  • 82
davids
  • 5,397
  • 12
  • 57
  • 94
  • This question should help you resolve your problem: http://stackoverflow.com/questions/9475893/how-to-add-reference-to-system-web-optimization-for-mvc-3-converted-to-4-app – competent_tech Oct 18 '14 at 23:47
  • I saw that post, but I can't modify the sub-application. Will it help to do it on the main site? – davids Oct 18 '14 at 23:49
  • 2
    upload all missing dlls to deployment folder on server, in this case upload `System.Web.Optimization.dll` – Koti Panga Oct 18 '14 at 23:57
  • 1
    Possible duplicate of [System.Web.Optimization and Microsoft.Web.Optimization won't load reference in vs 2012](http://stackoverflow.com/questions/16179293/system-web-optimization-and-microsoft-web-optimization-wont-load-reference-in-v) – Homer Dec 04 '15 at 18:10

6 Answers6

60

I just faced this issue now:

Error

I reinstalled the optimization framework and everything worked fine:

Install-Package Microsoft.AspNet.Web.Optimization
TylerH
  • 20,799
  • 66
  • 75
  • 101
Shubh
  • 6,693
  • 9
  • 48
  • 83
  • 2
    I like this solution and I usualy use it. Anyway somethimes you can recieve a message "Package 'Microsoft.AspNet.Web.Optimization' already exists in the project" and still not to have the problem solved. In that case just delete it from the "packages" folder and install again. – Marin Popov Apr 24 '18 at 08:22
  • I had the same issue. I didn't need this package in my project, but every time I tried to start the app I got this error. The solution id: look for this import and remove it (if needed) in Views/Web.config file. – Timofey Orischenko Oct 15 '18 at 12:36
39

I faced same problem after create a new area in MVC5. There is auto generate web.config file. Check your web.config file under Views folder there is a line

<add namespace="System.Web.Optimization" />
  1. if you need this then install it using following command.

    Install-Package Microsoft.AspNet.Web.Optimization
    

    at Package Manager Console like below

    pmc

  2. If you think no need then simply remove <add namespace="System.Web.Optimization" /> line from following section in web.config file

    <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="AutoParts.Web" />        
    </namespaces>
    
reza.cse08
  • 5,938
  • 48
  • 39
9

Check the Bin folder for the legacy application. It may simply need you to drop in that DLL. The System.Web.Optimization namespace exists as of ASP.NET 4.5 so it may not even be supported. The legacy App might be older than that.

TylerH
  • 20,799
  • 66
  • 75
  • 101
beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
5

In Visual Studio .. Go to your project and see the References tree.

right click System.Web.Optimization and choose Properties

look for the path , you will find it empty if that is the case then look in your PC for the file names System.Web.Optimization.dll and copy it in the bin folder of your project and that will resolve it

stackunderflow
  • 3,811
  • 5
  • 31
  • 43
  • 2
    It works for me, actually when I open References tree, these issue libs are automatically loaded. Thanks! – Vincent Wen Jan 15 '21 at 09:45
  • same. after i open the references window and close it the references automatically get updated after copying the file to the bin folder. – Joshua K Jan 05 '23 at 19:29
1

I was just experiencing: "CS0234 C# The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)" and found this stackoverflow page. -- The cause for me was due to me renaming the web form (aspx page) I was working with, I also wanted to rename the public partial class behind that form. Doing so resulted in the mentioned error code. I added "using System.Web.Optimization;" to the code behind (C#), "cleaned" the solution, then "built" then solution, then "rebuilt" the solution and now the error is gone and that using statement wasn't being used, so I got rid of it. My solution no longer has that or any errors so I figured I'd share this weird story. I probably didn't need to use that "using" statement, but whatever.

Eric Milliot-Martinez
  • 4,076
  • 3
  • 23
  • 28
-4

Main to Area

@Html.ActionLink("My Area Home", "Index", "Home", new { area = "MyArea" }, null)

Area to Main

@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
Pedram
  • 15,766
  • 10
  • 44
  • 73
DevOps
  • 1