混乱在Asp.net Web项目的根命名空间 [英] Confusion with Root Namespace in Asp.net web project

查看:247
本文介绍了混乱在Asp.net Web项目的根命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个项目,WebUI和域名的VS2008的解决方案; WebUI中引用域

I have an VS2008 solution with 2 projects, WebUI and Domain; WebUI references domain

在WebUI项目中的根命名空间是:MyCompany.MyProjectName.WebUI结果
在域项目的根命名空间是空白。 (命名空间手动声明所有类)

The Root Namespace in the WebUI project is:MyCompany.MyProjectName.WebUI
The Root Namespace in the Domain project is blank. (Namespaces are manually declared for all classes)

所以,一切已经工作正常,直到我试图通过一个完全合格的路径来引用域A类:
MyCompany.MyProjectName.Domain.EntityClasses.SomeClassName

So everything has been working fine, until I tried to reference a class in Domain via a fully qualified path: MyCompany.MyProjectName.Domain.EntityClasses.SomeClassName

VS无法找到类。
但是,如果我在code文件的顶部做:
进口MyCompany.MyProjectName.Domain.EntityClasses

VS was not able to find the class. However, if I do at the top of the code file: Imports MyCompany.MyProjectName.Domain.EntityClasses

....然后就可以看到它。 (正如SomeClassname,而不是完全合格的名称)

....then it can see it. (Just as SomeClassname, not the fully qualified name)

这是否有什么意义?

所以,后来我明白我WebUI中的根命名空间,瞧,完全合格的声明,然后做的工作。但是,这样做似乎再失效我的网页上我所有的用户控件的注册。这似乎解决这​​个问题唯一的办法就是在每个用户控件的codebehind,手动添加MyCompany.MyProjectName.WebUI的命名空间
...这可能使意义,因为也许是页面的命名空间已经莫名其妙地仍然保留根命名空间的价值。但是,如果我是创建一个全新的aspx页面和拖放用户控件(这之前手动添加命名空间),即使该页面无法正常注册。然而,uwer控制得当设计视图渲染....所以VS UI似乎能够妥善解决它,但是编译器貌似不能。

So, then I cleared my WebUI Root Namespace, and voila, the fully qualified declaration then does work. However, doing this then seemed to invalidate the registration of all my user controls on my pages. The only thing that seemed to solve this was in the codebehind of each user control, manually add a namespace of MyCompany.MyProjectName.WebUI ...which might make sense as perhaps the namespaces of the pages somehow had still retained the root namespace value. But, if I was to create a brand new aspx page and drop a user control on (this is before manually adding the namespace), even that page couldn't properly register it. Yet, the uwer control properly rendered in design view....so the VS UI seemed to be able to properly resolve it, but the compiler seemingly can't.

因此​​,从我可以告诉,我至少可以得到的东西在正确的命名空间手动包装用​​户控件工作。为什么这是必要的aspx页面,即没有指定命名空间,就看用户的控制,似乎就没有任何意义。

So from what I can tell, I can at least get things to work by manually wrapping user controls in the proper namespace. Why this is necessary for aspx pages, that have no namespace specified, to see the user controls, seems to make no sense.

有没有我误解?一些基本原则。

Is there some fundamental principle I am misunderstanding??

推荐答案

好吧,我想这就是问题所在:

Ok, I think this is the problem:

当你有你的项目中指定一个根命名空间,创建一个新的aspx页面,ASPX页面硬code盘根命名空间,其定义的在创建时,即:

When you have a root namespace specified for your project and create a new aspx page, the aspx page will hardcode the root namespace in its definition at creation time, ie:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/DefaultMasterPage.Master" CodeBehind="Home.aspx.vb" Inherits="yourCompany.yourApp.WebUI.Home" %>

...这绝不会即使您更改项目的根命名空间的改变。如果你没有一个根名字空间的创建页面时后,你会得到:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/DefaultMasterPage.Master" CodeBehind="Home.aspx.vb" Inherits=".Home" 

所以,如果你通过改变根命名空间半路上,所有的前aspx页面都会有自己的命名空间硬coded到旧值。然而,我的认为的背后code为ASPX的确实反应,去除根命名空间,所以你开始得到与用户控件等怪异的行为而你必须进入所有code的屁股,并明确设置匹配的命名空间那里的页面能够正常工作。在这种情况下,我认为做的最好的事情是搜索所有的aspx文件的名称空间的这个明确定义(在继承的标签),并删除它。你应该,我认为,仍然能够明确设置命名空间中的codebehind如果你选择,其中ASPX将继承。不是100%肯定,但。

So if you change the root namespace half way through, all the prior aspx pages will have their namespace hardcoded to the old value. However, I think the code behind for the aspx does react to the removal of the root namespace, so you start to get weird behaviour with user controls, etc. and you have to go into all the code behinds and explicitly set a matching namespace in there for the pages to work properly. In this case, I think the best thing to do is search all your aspx files for this explicit definition of the namespace (in the Inherits tag) and remove it. You should, I think, still be able to explicitly set the namespace in the codebehind if you so choose, which the aspx will inherit. Not 100% sure though.

长话短说,决定要在开始该命名空间的命名方法,并坚持下去。

Long story short, decide which namespace naming method you want at the start and stick with it.

这篇关于混乱在Asp.net Web项目的根命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆