如果设置了 MasterType,如何修复自动生成的 Master 属性的命名空间问题
在遇到这个问题几周后,我终于决定寻求以下问题的解决方案:
在 .aspx 页面中,您可以设置
<%@ MasterType VirtualPath="~/Mastername.master" %>
这会导致 .aspx.designer 中自动生成的属性
public new Mastername Master {
get {
return ((Masternamee)(base.Master));
}
}
工作得很好。但是,如果我在 .aspx 文件中进行更改,该属性将是新的自动生成的,如下所示:
public new NAMESPACE1.Mastername Master {
get {
return ((NAMESPACE1.Mastername)(base.Master));
}
}
之后将无法进行编译,因为 MasterPage 的类无法在给定的命名空间中解析。 母版页将 NAMESPACE1 作为命名空间。
每个内容页都有相同的 NAMESPACE1。 自动生成的属性尝试在 NAMESPACE1.NAMESPACE1 中查找母版页类,该类将失败,因为它不存在。当然我可以删除第一个NAMESPACE1。使应用程序再次可编译,但几乎每次我在 .aspx 文件中进行更改时都这样做很糟糕。
有办法避免这个问题吗?我能想到的唯一方法是忽略自动生成的属性,并在每次我想要访问母版页时进行显式转换。
编辑:我正在使用 Visual Studio 2008 Professional SP1。
after weeks of having this issue I finally decided to ask for a solution to the following problem:
In the .aspx page you can set
<%@ MasterType VirtualPath="~/Mastername.master" %>
This results in an auto generated property in the .aspx.designer
public new Mastername Master {
get {
return ((Masternamee)(base.Master));
}
}
Works perfectly fine. But if I do changes in the .aspx file, the property will be new auto generated and it looks like the following:
public new NAMESPACE1.Mastername Master {
get {
return ((NAMESPACE1.Mastername)(base.Master));
}
}
Compiling will not be possible afterwards, because the class for the MasterPage cannot be resolved at the given namespace.
The masterpage has NAMESPACE1 as namespace.
Every contentpage has the same NAMESPACE1.
The autogenerated property tries to look for the masterpage class in NAMESPACE1.NAMESPACE1 which will fail, due to it does not exist. Of course I can remove the first NAMESPACE1. to make the app compilable again, but it just sucks to do this nearly every time I make changes in the .aspx file.
Is there a way to avoid this problem? The only way I can think of, is to ignore the auto generated property and make a explicit cast everytime I want have access to the masterpage.
Edit: I'm using Visual Studio 2008 Professional SP1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于某种原因,设计者认为母版页是在命名空间
NAMESPACE1
中定义的,因此请查看母版页定义(和后面的代码)以检查其命名空间是否已被修改(可能是意外)。如果没有任何明显的信息,则可能需要在所有文件(*.cs、*.aspx、*.master...)中搜索
NAMESPACE1
。(这就是使用 VCS 会有所帮助的地方——您可以检查更改的历史记录。)
For some reason the designer believes that the master page is defined in namespace
NAMESPACE1
, so look at the master page definition (and code behind) to check its namespace has not been modified (possibly accidentally).If there is nothing obvious, a search in all files (*.cs, *.aspx, *.master, ...) for
NAMESPACE1
may be needed.(This is where using a VCS would help --- you could check the history of changes.)
实际上,这更多的是设计师的“功能”。 ;-)
设计器文件中使用的主名称将从 .Master 文件的 Inherits 属性中提取。因此,更改限定 Inherits 属性的方式,这将更改创建设计器文件时使用的类名称。
Actually it's more a designer "feature". ;-)
The Master name used in your designer file will be pulled from your .Master file's Inherits property. So change how you qualify the Inherits attribute, and that will change the class name used when the designer file is created.
我找到了一个有效的解决方案。我不会在设计器文件中使用自动生成的属性。我将编写自己的包装器属性,并在每个内容页中实现该属性。
I found a solution that works. I won't use the autogenerated property in the designerfile. I'll write my own wrapper property that I do implement in every contentpage.
当我将
<%@ MasterType VirtualPath="~/TestMaster.Master" %>
添加到 SOURCE 视图中的 aspx 页面时,我遇到了同样的问题。由于某种原因,该页面从未正确创建,并且一直给我无效的命名空间错误,直到我实际更改为“设计”视图并调整控件大小,最后错误消失了。它在某个地方使用了一些缓存的数据(即使构建/清理解决方案也没有清除它),并且在设计者重新创建页面之前,它会生成该错误。I had this same problem when I added
<%@ MasterType VirtualPath="~/TestMaster.Master" %>
to my aspx page in SOURCE view. For some reason, the page never created correctly and kept giving me invalid namespace errors until I actually changed to DESIGN view and resized a control and finally the error went away. Somewhere it was using some cached data (even a Build/Clean Solution didn't clear it out) and until the designer recreates the page, it generates that error.改成
这样
就可以完美运行
Change
to
this will work perfectly