Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to convert a Website project to a Web Project

UPDATE: All of these problems was for converting a Website application to a Web Project in Visual Studio. Once I dug in and I found the solution, as I marked as answer below.

Basically, if you are wanting to upgrade/move your Website project to a Web Project, you will run into a number of errors. The short how-to is in the answer below. Thank you all for comments and help.

Old question and title is below:

How to wrap namespace xx {} to all class files?

How would you wrap namespace to all codebehind files in a webproject (or any project)?

namespace MyDomain
{
  // regular code here
}

Long story short...

I have been converting a project that was outsourced overseas. Over 20,000 files in the 'web' project alone, for such a simple website (shakes head). It was written in vs2005 using VB.NET and C# (yes, both complilations). It was originally a Website applicaiton. I have converted it to a Web Application, mainly because of the 8,000 "temp" assemblies that are produced when you publish.

Ok, I am converting to VS2008. I've converted all code to C#. Looks good. I've brought over all special App_Code/VB and C# code into seperate assemblies.

Now when I attempt to compile again, I am alerted to a huge number of errors. I fix these by wrapping the codebehind files in a Namespace, and updating the usercontrol to use the new namespace.

But... 5000+ errors to go... Help!

alt text http://eduncan911.com/blog/thumbnail/4-15-2009.6-00-13.PM.png

Maybe there is another way? Here is a sample snippet from the codehind:

public partial class MyHomepage_MyStuff_MyPosts : SecurePage
{ ... }

Notice the class name MyHomepage_MyStuff_MyPosts. This is the directory path to the location of the file at /MyHomepage/MyStuff/MyPosts.aspx.

The error is that the codebehind file cannot find a reference to the control being used. Such as:

ctl_website_rpt.RowCount = _totalCount;

This is a control (their naming convention) called "ctl_website_rpt" within the ASPX file. The aspx page has in the header:

<%@ Page Title="My Posts" Language="C#" 
  MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" 
  CodeFile="MyPosts.aspx.cs" 
  Inherits="MyHomepage_MyStuff_MyPosts" %>

Maybe there is a way I can reference this page elsewhere? Without changing it to the namespace of?

namespace Website.MyHomepage.MyStuff
{
  public partial class MyPosts
  { ... }
}

<%@ Page Title="My Posts" Language="C#" 
  MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" 
  CodeFile="MyPosts.aspx.cs" 
  Inherits="Website.MyHomepage.MyStuff.MyPosts" %>
like image 495
eduncan911 Avatar asked Apr 15 '09 22:04

eduncan911


2 Answers

Fyi, I found the solution. It's easier than you think.

You simply right-click your new Web Project and there is an option for "Convert to Web Application." This does some interesting things. First, it creates the [file].aspx.designer.cs auto-generated file. This got rid of 99% of my errors, as it was because the codebehind could not find a reference to the control (see screenshot in my question above).

This also re-writes the <%@ Page %> decleration in your aspx file to to inherit from the code behind's new name/namespace.

All that was left was some assembly reference issues and done!

The How To is:

1) Create a new Web Project, copy all files from your old Website project to the new Web Project.

2) Fix all references to your assemblies you KNOW you will need.

3) Right-click on the Web Application and select Convert to Web Application. Closely inspect the error and warning log. Re-convert at will once you fix things.

If any aspx or ascx files do not have a designer.cs file, then the conversion failed on that file. Fix the references, namespace, etc and re-convert.

Tip #1: Make sure you have all references added before you Convert to Web Application. Sometimes a number of errors can happen.

Tip #2: Even though it looks like it completed, look for errors after the convert. It will point you to files it was unable to convert, so you need to work on them.

Tip #3: You can re-convert any sub-directory, or any file, any number of times. Say you had multiple errors in #2 you were working to fix, you can re-convert that file or directory multiple times until you fix it. Re-converting does not hurt the existing application.

like image 97
eduncan911 Avatar answered Oct 03 '22 07:10

eduncan911


I can't think of a solution other than writing a little console app that would go through all of the files and modify them for you.

like image 45
David Avatar answered Oct 03 '22 06:10

David