6

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.

2
  • This is dependant on the project type. Are you creating a new "Web Application" or creating a new "Web Site"? Commented Aug 9, 2009 at 15:12
  • That still doesn't answer the question very much, what type of web site did you add? There are two, web sites and web applications (I can't be more exact, I don't have 2005 on this machine) Commented Aug 9, 2009 at 15:17

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to inherit .aspx pages.
You mean u have a basepage.aspx with a codebind class "basepage". And you have a newpage.aspx which has a "newpage" class inherting "basepage"?If that is so, read this: west-wind.com/WebLog/posts/3016.aspx under the section "What to watch out for". Hope this helps.
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.

4 Comments

Which planet do you live in? There are no namespace declarations in any asp.net code-behind file.
Are you kidding? Give me a minute, I'll get you an example.
I am using VS2005 and I never saw this happening in my Web projects.
I did this example in VS2008 Pro and I believe I saw more or less the same thing when I was using VS 2005. Maybe you were creating web site projects as opposed to web application projects? There the organization is somewhat different.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.