ASP.NET Namespace

What is the namespace the Default.aspx page resides in when I create an ASP.NET project?

And how to find the namespace of any other ASP.NET page in the project?

I am using VS2005. I first created a blank solution and then added a webSite to it.

When I click right-button and go to ‘Add New Web Site’ – menu I find the following template ASP.NEt WebSites(1st template), then I added this to my sln.

I am using C# and VS2005. This will not match VS2008 in this case.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Web Sites do not have/support namespace. Web Applications do.

To answer the question, since a new ‘Web Site’ is created, namespace doesn’t come into play. As to how to access other WebForm page classes from a Webform page, I haven’t figure a reason for that. And I do not think it is do-able (correct me if I’m wrong).

Anyway there are ways to get around. If you have some reuable business/UI/other logics used within a class of one the webforms, simply move those methods out to say XXX.cs file under App_Code. There is no need for namespace and you can use it within all the webform classes.

Method 2

Look at the code-behind (Default.aspx.cs in your case) of a page in question. There you will see your namespace. Aspx is an addition to the class in code-behind that is merged using the partial class declaration.

I just created a new web application project. This what the code-behind looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }
    }
}

So you see the “WebApplication1” namespace. You see it, right?

ADDED: So I created a web site project again to check that out. Okay, I confirm, I do not see any namespace declarations there. After googling a little bit I found this post:

asp.net – Web Site vs. Web Application (link fixed)

The new compilation model threw out the visual studio project file itself, took asp.net back to the “compile-on-the-fly” concept, all but eliminated the use of namespaces within a web site, and radically altered the way UI template and the associated code-behind were arranged.

From the looks of it, it just throws all classes together, both page classes and your custom logic classes you usually put into App_Code folder. Class viewer also does not show page objects even if I wrap them in my custom namespaces, but it does so correctly along with the namespaces for declarations in the App_Code folder. I suppose the guys in VS team didn’t mean you to care about namespaces for page classes.

Method 3

Try defining an interface in the app code directory and have your code behind file implement the interface.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x