简体   繁体   中英

ASP.NET compiler can't find any namespace

I have a foo.cs file

namespace LoL
{
    public class testing2
    {

    }
}

and in

Default.aspx.cs file:

UPDATE: I don't know if it can help really, but here's all Default.aspx.cs file:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LoL;

namespace WebApplication4
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

and I get:

Compiler Error Message: CS0246: The type or namespace name 'LoL' could not be found (are you missing a using directive or an assembly reference?)

For any class that I try to use. Not libraries only. I tried several solutions moving/copying files, on VS: Porject > add existing file > foo.cs etc

but no luck in any of them.

How to fix this? I have no idea how to fix this.

NOTE: If I try by using F5 mode in VS, it work, but put the website on C:\\inetpub\\wwwroot\\ give this error as in the host.

EDIT: By error message on ASP.NET page I can see that IIS is calling C# compiler 4.0, but I'm targeting .NET framework 3.5 on my VS 08 (the last version avaliable on VS 08). Can this have some relation to my problem?

EDIT2: I'm able to compile my web site project from command line sucessfully. With msbuild program on project path. It just doesn't work when IIS try to compile it. The IIS don't know about foo.cs file.

EDIT3: Now I'm using aspnet_compiler (in cmd.exe) program:

C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\aspnet_compiler -v webApplication4

On path: C:\\inetpub\\wwwroot\\webApplication4

I'm getting same error. So, I looked at Visual Studio configuration stuff, it's the .csproj file, I was looking for the inclued files passed by compiler (generated by VS).

I found them on ItemGroup tag:

  <ItemGroup>
    <Compile Include="foo.cs" />
    <Compile Include="Default.aspx.cs">

As your can seen, the foo.cs is included to be compiled. So, the file is there. Why aspnet compiler can't find any namespace?

If you created a webSite and not a webApplication you need to put your .cs files in the special ~/App_Code folder and deploy them on the server. Please read about the differences between a website and a precompiled web application on MSDN : http://msdn.microsoft.com/en-us/library/dd547590.aspx .

Is LoL in a different project than your website? If so, you need to add a reference to that project in your website project. In the website, right click on the References folder and click "Add References" and try building again.

If that doesn't work, your LoL project might not be set to build. Verify in the build output that it is building successfully. If it isn't, you need to look at your build configurations and verify for your currently active configuration that it is set to build.

Try to copy your compiled class libraries in the GAC, using gacutil.exe. Open VS command prompt then type :

gacutil.exe -i "assemblypath.dll"

@Machinegon Because of deployment, and for management purposes. Unless it's classes multiple sites use on the server, there's no reason to deploy a web site's DLLs to GAC. Even if used by multiple sites, it's best to have the version the site's known to be compatible with in its bin folder, otherwise registering new versions could break other sites/applications.

I know this is an old thread, but it is a common problem. Please note the following. You cannot have more than one bin directory in your website. The bin directory must live in the root folder of your website. You cannot have subdirectories that contain bin directories. Often times people will create a webpage in Visual Studio using a new project and then they will publish that webpage to a subdirectory of their website. When the webpages published, a new bin folder will be placed in that subdirectory. That bin directory is useless and will not be referenced by anything. The webpage in the subfolder, will attempt to look for any dependencies in the bin directory that is located in the root of the website. This is likely something set up by the service provider, or it may simply be the default behavior of IIS/ASP.net. In any event when you publish a website you must publish the entire site. That means everything. The publication process must include everything from the root directory on out. If you add a dependency to a webpage that is in a subfolder, the dependency must be added to the website, the entire website at root folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM