missing a using directive or an assembly reference

2019-09-11 03:51发布

问题:

So I have an ASP.NET website, which works great on my machine. But when I deployed it on web host server, its giving me an error saying:

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: CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 15: using Microsoft.VisualBasic;
Line 16: using System.Data.SqlClient;
Line 17: using Newtonsoft.Json;
Line 18: using System.Globalization;
Line 19: using System.Text.RegularExpressions;

I think this is a classic error, and there are similar questions already being asked, even by me before. But, all the solutions are either not clear or not applicable.

Since its a "website" project, so the property of the aspx.cs file does not contain any field such as "Build Action" or "Copy Local" etc. I am using Newtonsoft to parse JSON. Can someone please suggest me STEP-BY-STEP way to get rid of this problem?

I came across solutions such as clean-up and such. Clean what? In my solution explorer, there's this BIN folder which contains the dll, xml, and pdb for the Newtonsoft library; which is AS-IT-IS copied on to the webhost server, using COPY WEBSITE tool.

What do I need to do to fix the problem? Thanks a bunch.

回答1:

This would happen if you're not deploying to an application root, but a normal windows folder.

The bin folder must be in your application root (~/bin) for assemblies to be resolved correctly. You may change this by changing your private probing path in your web.config-file, but that's a bad solution for this.

Ensure you're uploading your application to an folder configured to host an application. This can be done in your IIS Manager if you have Remote Desktop access. Shared hosting solutions may provide this functionality in a web-based configuration tool. Log into your "control panel" and look around.

Or you could just contact your hosting support.



回答2:

Take these steps: -> Solution explorer window -> right mouse on your .cs file in Visual Studio -> select Properties -> Build action = Compile

or

Remove reference of your Newtonsoft and copy Newtonsoft.dll to your bin folder and give a reference from there.



回答3:

Build your website and publish it to some folder. Then copy all requisite files to your web server. Just check whether you got Newtonsoft dll in bin folder (web server) or not. If you do not have, then copy it from local machine and paste it to web server.