Confusion with Root Namespace in Asp.net web project(与 Asp.net Web 项目中的根命名空间混淆)
问题描述
我有一个 VS2008 解决方案,有 2 个项目,WebUI 和 Domain;WebUI 引用域
I have an VS2008 solution with 2 projects, WebUI and Domain; WebUI references domain
WebUI项目中的Root Namespace为: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)
所以一切正常,直到我尝试通过完全限定的路径引用 Domain 中的一个类: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 无法找到该类.但是,如果我在代码文件的顶部这样做:导入 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 根命名空间,瞧,完全限定的声明就可以工作了.但是,这样做似乎会使我页面上所有用户控件的注册无效.似乎解决这个问题的唯一方法是在每个用户控件的代码隐藏中,手动添加 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 页面将在其定义中硬编码根命名空间在创建时,即:
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" %>
...即使您更改项目根名称空间,这也永远不会改变.如果您在页面创建时没有根名称空间,您会得到:
...and that will never change even if you change the project root namespace. If you didn't have a root name space when the page was created, you'd get:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/DefaultMasterPage.Master" CodeBehind="Home.aspx.vb" Inherits=".Home"
因此,如果您在中途更改根命名空间,所有先前的 aspx 页面都将其命名空间硬编码为旧值.但是,我认为aspx 背后的代码确实 会对根命名空间的删除做出反应,因此您开始使用用户控件等出现奇怪的行为,并且您有进入所有代码背后并在其中显式设置匹配的命名空间,以使页面正常工作.在这种情况下,我认为最好的办法是在所有 aspx 文件中搜索命名空间的显式定义(在 Inherits 标记中)并将其删除.我认为,如果您愿意,您仍然应该能够在代码隐藏中显式设置命名空间,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 项目中的根命名空间混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:与 Asp.net Web 项目中的根命名空间混淆
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01