Search code examples
c#asp.netnamespacesvs-web-application-project

ASP.NET Website to Web Application conversion issue - Could Not Load Type 'ClassName' AND Set Namespaces for all existing webpages technique


I have converted one huge Website Project into a Web Application Project as we want to have Compiled code in form of DLL for security purposes and followed below MSDN articles:

http://blogs.msdn.com/b/webdev/archive/2009/10/29/converting-a-web-site-project-to-a-web-application-project.aspx

http://msdn.microsoft.com/en-us/library/aa983476%28v=vs.100%29.aspx

The website is having 1000s of web pages/files. I have resolved all the issues of duplicate references, class name colisions and some compilation errors faced at on Build Solution stage. Due to the earlier website pattern, class names are in the following pattern:

for e.g. if UserMaster is in admin/Operations/Masters folder so the class name will be like: admin_Operations_Masters_UserMaster.

Now, Web Application is Building Successfully and when I try to run the application, it shows "Could Not Load Type 'ClassName'" so I found out that the issue of because Namespaces are absent due to earlier Website Pattern.

Found out in the MSDN:

By default, pages and classes that are built by using the Visual Studio Web site project model do not automatically include a code namespace. However, pages, controls, and classes that are built by using the Visual Studio Web application project model automatically include a code namespace. When converting the Web site project to a Web application project, you will have to add the namespaces to the code.

I have DEFAULT NAMESPACE in the project properties.

So I tried to add the namespaces to whole of the project via ReSharper -> Refactor -> Adjust Namespaces but its for .cs files only and not for .aspx.cs etc AND so its not working.

I thought I need to make a utility to add the namespaces automatically in pages as per their folder path structure but its a time consuming process.

So need a workaround to add the namespaces automatically in pages as per their folder path structure.

Secondly,

What I did for testing is, I have wrapped the default namespace in login page of the project where I faced 'could not load type issue', added in Inherits attribute also. Then did a Clean Solution then Rebuild Solution. But still getting the same issue.

I don't why DLL is not getting updated with the login page's class.

The strange thing is when I change the Codebehind to CodeFile (attribute of Page Directive) then the page is working very fine. Its due to source file is present at the time of running the project BUT i want to use Codebehind attribute as it will tell the compiler to look for the class in DLL not in Source file.

Finally, Summary is :

I have two major issues:

  • Could not load type 'className'
  • How to add the namespaces automatically in pages as per their folder path structure.

I have already searched here and came around different topics but nothing works for me. one have suggested to use "Surround With" feature of VS but I need to go one by one on each file which is NOT possible right now.

So Tried:

  • Set path of DLL to "bin" instead of "bin\debug"
  • Checked the Build Configuration and its of x86. also the DLL is present in bin
  • Checked all project properties and all are fine. Cleaned and rebuild solution hundreds of times. Cleaned Temporary ASP.NET files also.
  • Set Build Action to "Compile" for .cs class files
  • Cannot set Build action to Compile for aspx pages as they are static html and they should be set to "Content" which is default Build Action.
  • Resharper solution for adding namespaces but not working

Please help....

Any help will be greatly appreciated. Thanks.


Solution

  • Solved the Issue:

    Earlier, I have tried to Set Build Action to "Compile" for .cs class files Only and for all the files sitewide to Compile from .csproj file but it was not working due to aspx page itself set to Compile which was not required.

    Now, I have changed the Buid Action to Compile which was Content for .aspx.cs and .aspx.designer.cs files.

    and it solved the issue.

    Like for e.g.:

    • Solution 'TestWebApp1'
      • Project 'TestWebApp1' (ASP.NET Web Application)
      • Properties
      • References
      • App_Data
      • Scripts
      • Default.aspx (Build Action: Content)
        • Default.aspx.cs (Build Action: Compile)
      • SiteLayout.Master (Build Action: Content)
        • SiteLayout.Master.cs (Build Action: Compile)
      • Web.config

    Source: None of my "code behind" code is being called