将命名空间添加到 ASP.NET MVC 3 视图
我尝试将命名空间添加到 ASP.NET MVC 3 应用程序的 web.config 中的 configuration/system.web/pages/namespaces
中,这样我就可以在视图中使用这些命名空间中的类,而无需 @using ,但这没有效果。如何将命名空间添加到我的视图中?
I tried adding namespaces to configuration/system.web/pages/namespaces
in the web.config of my ASP.NET MVC 3 application so I could use classes in those namespaces in my views without needing a @using, however this has no effect. How can I add namespaces to my views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVC razor 有一个不同的命名空间区域。
查看第二个 web.config,即 Views 文件夹中的那个,并以这种方式添加命名空间。
MVC razor has a different area for namespaces.
Look in the second web.config, the one in your Views folder and add namespaces this way.
我不确定为什么它如此不稳定,但很可能它实际上正在工作,但 Visual Studio 无法识别它,直到您关闭并重新打开您所在的视图。同时确保您位于 Views 目录中的 web.config 中。
I'm not sure why it's so flaky, but chances are that it is actually working, but Visual Studio doesn't recognize it until you close and re-open the view you're in. Also make sure you're in the web.config that's located in the Views directory.
我使用此方法将资源文件包含到我的视图中,但 Razor 不想接收我的课程。经过一番纠结后,我意识到我没有将
Resources.resx
文件上的访问修饰符更改为公共(默认情况下它创建为内部)。一旦我改变了它&重新编译后,我可以从所有视图访问我的资源。菜鸟错误,不愿意看到它发生,但希望它能为某人减轻一些痛苦。
I used this method to include Resource files into my views but Razor didn't want to pick up my classes. After some hair-pulling I realized I didn't change the Access Modifier on the
Resources.resx
file to public (it's created as Internal by default). Once I changed it & recompiled, I could access my resources from all views.Rookie mistake, hate to see it happen, but hopefully it'll save someone some pain.