locked
Deploying Website error CS0246: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference? RRS feed

  • Question

  • User-759001637 posted

    Hello, 

      I am currently deploying an ASP.Net Website(Not Web application) and met this problem: CS0246: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?).

      The Visual Studio I used to write my codes are Microsoft Visual Studio Community 2015. I am using an ASP.Net Website, not the Web Application. I put two CS files( e.g.SQLHelper) into the folder App_Code. When I started this project, the .NetFramework I used is .Net 4.5.2. ( Ididn't have much idea of the version of .Net Framework at that time). Later I came into realized that this may cause an issue. So when I build the website, I set the target Framework to .Net 4.0 so as to match my Virtual Host. (I did this by right click website ->property page->Build->target Framework-->choose 4.0).

    As for Deployment, I used the right click-->publish Web-->publishMethod:FTP-->fill out username and password. I left the desination URL blank since I do not know how to fill it.   The FTP I had is provided by Alibabacloud(I bought a Cloud Virtual Host from Alibabacloud). I already set the .Net Framework settings of my Virtual Host to ASP.NEt 4.0 Integrated mode.

    The website run on my computer fine. But after deployment, when I type the logOn page in the Browser, it showed the error Website error CS0246: The type or namespace name 'UserGate' could not be found (are you missing a using directive or an assembly reference?).UserGate is the class I defined in the CS file in App_Code foler. So I add namespace MyClass to the CS File and added Using MyClass at the top of ever .aspx.cs file in my project. Then I requested the LogOn page in Browser again and found the error is "The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?". So I guess somehow the contents of App_code cannot get run. 

    What shall I do?

    Oh, BTW, the name of my project is called TicketSystemWeb, but somehow in the Solution Explorer, Under the solution'TicketSystemWeb(1 project), it shows the earth-like image and the name TicketSystemWeb(2). It automatically added (2). I do not know whether the name caused this issue. 

     Below is the Web Configuration.

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <connectionStrings>
        <add name="bds_dbConnectionString" (I omit the username and password here) providerName="System.Data.SqlClient"/>
      </connectionStrings>
      <system.web>
        <compilation debug="false" targetFramework="4.0">
         
          <assemblies>
            <add assembly="System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          </assemblies>
        </compilation>
         <customErrors mode="Off"></customErrors>
        
         
        <httpRuntime/>
        
      </system.web>
    </configuration>

    Monday, April 27, 2020 4:19 AM

All replies

  • User288213138 posted

    Hi ZZHCAT,

    But after deployment, when I type the logOn page in the Browser, it showed the error Website error CS0246: The type or namespace name 'UserGate' could not be found (are you missing a using directive or an assembly reference?).UserGate is the class I defined in the CS file in App_Code foler. So I add namespace MyClass to the CS File and added Using MyClass at the top of ever .aspx.cs file in my project. Then I requested the LogOn page in Browser again and found the error is "The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?". So I guess somehow the contents of App_code cannot get run. 

    The App_Code file contains source code for shared classes and business objects that you want to compile as part of your application. In a dynamically compiled Web site project, ASP.NET compiles the code in the App_Code folder on the initial request to your application. Items in this folder are then recompiled when any changes are detected.

    You can right click on the .cs file in the App_Code folder and check its properties. Make sure the "Build Action" is set to "Compile".

    After you change Build Action Property of the *.cs source file to Compile from Content, the namespace was resolved in other folder locations of the project.

    Best regards,

    Sam

    Tuesday, April 28, 2020 1:58 AM
  • User-759001637 posted

    Hello Samwu, 

    Thank you for your reply! I tried your answer, but really the CS File in the folder of App_Code of WEBSITE do not have the "property"(see this image). So I cannot find anyplace to find place to change "BuildAction". WebSite-CS

    I created a WebApplication and add the folder App_Code and add a CS file. RIghtclicking the CS file I can find the property-->BuildAction->Content.(see the image)

    WebApplication-CS

    But my solution is in WEBSITE instead of WebApplication.  These two are  different in some areas. So I guess I need other ways to fix my problem. But Thanks anyway., 

    BTW, is there Specialist from Microsoft ASP.Net team  on the forum?

    Thursday, April 30, 2020 4:58 AM
  • User288213138 posted

    Hi ZZHCAT,

    Thank you for your reply! I tried your answer, but really the CS File in the folder of App_Code of WEBSITE do not have the "property"(see this image). So I cannot find anyplace to find place to change "BuildAction". 

    You can try to move your cs file out of the App_Code folder and put it in another folder for testing.

    Best regards,

    Sam

    Thursday, April 30, 2020 8:01 AM
  • User-759001637 posted

    Hi Samwu, 

        I tried this, add a folder called "MyCode" and put these two files in. However, right click the .cs file still have no "property". (see this image)

       I found that right click has an item called"publish "SQLHelper.CS"(publish ".cs file name"), So I clicked this for both .cs files. Then I publish my website again using the original method mentioned in my initial post. But still meet the same issue " CS0246: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?)"

      BTW, is there any person from Microsoft Visual Studio on the forum to answer this question?  It is their product and I think "website" is their feature, it should be easily published. 

    put into another folder

    Wednesday, May 13, 2020 2:55 AM
  • User288213138 posted

    Hi ZZHCAT,

    I found that right click has an item called"publish "SQLHelper.CS"(publish ".cs file name"), So I clicked this for both .cs files. Then I publish my website again using the original method mentioned in my initial post. But still meet the same issue " CS0246: The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?)"

    This link is the situations cause compiler error CS0246.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0246

    Please check if you have the same situation in your project.

    Best regards,

    Sam

    Thursday, May 14, 2020 8:36 AM