ASP.NET Namespace

2020-02-06 06:59发布

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.

3条回答
混吃等死
2楼-- · 2020-02-06 07:36

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.

查看更多
在下西门庆
3楼-- · 2020-02-06 07:38

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

查看更多
家丑人穷心不美
4楼-- · 2020-02-06 07:40

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.

查看更多
登录 后发表回答