ASP.NET MVC 3、Razor 视图和可移植区域
我正在尝试将可移植视图与 ASP.NET MVC 3 和剃刀视图一起使用,因为这似乎是创建简单插件架构的最佳方法。所以我有我的类库设置,我的视图位于 /Views/Admin/Index.cshtml 中,并将其设置为嵌入式资源。然后,我将该项目作为主 Web 应用程序项目的依赖项包含在内。当我尝试访问管理控制器的索引操作时,我收到一条消息,指出找不到该视图文件(因此控制器已正确包含)。我假设它正在尝试查看主 Web 应用程序项目而不是可移植区域二进制文件。有没有办法让剃刀视图与便携式区域一起使用?
I am trying to using portable views with ASP.NET MVC 3 and razor views as that seems like the best way to create an easy plug-in architecture. So I have my class library setup and I have my view located in /Views/Admin/Index.cshtml and it is set as an Embedded Resource. I then include that project as a dependency for the main web application project. When I try to access the Admin controller, Index action I get a message that is can't find that view file (so the controller is being properly included). I am assume it is trying to look in the main web application project and not the portable areas binary. Is there a way to get razor views to work with portable areas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经在这个特定问题上苦苦挣扎了一段时间,但我想我终于弄清楚了。
文件夹结构以及项目内命名空间的调用方式对于其正常工作非常重要!
我在这里有一个带有嵌入式剃刀视图的便携式区域的工作示例:
看一下项目的结构。
该区域的名称为
UserAdministration
,项目根目录中有一个UserAdministrationRegistration
类,该类位于UserAdministration
命名空间中。然后在
Views
文件夹下有一个Controllers
、Models
和Views
文件夹(就像普通的 MVC 项目一样) ,还有一个UserAdministration
文件夹,其中包含该区域的视图。还有一些对于嵌入式视图的工作非常重要的事情:您必须在
global.asax.cs
文件的Application_Start
方法中注册一个新的视图引擎,你这样做吗?并且...在您的注册类中,请确保重写带有 2 个参数(
AreaRegistrationContext context
和IApplicationBus 总线
)的RegisterArea
方法,并且在那里调用基本实现:如果您不调用基本实现,则必须至少添加一个
以确保您的嵌入视图和资源已注册。
I have been struggling on this particular issue for a while, but I think I finally figured it out.
The folder structure and how the namespaces are called inside your project is very important for this to work properly!
I have a working example of a Portable Area with embedded razor views here:
Take a look at the structure of the project.
The area's name is
UserAdministration
, and there is aUserAdministrationRegistration
class in the root of the project, which resides in theUserAdministration
namespace.Then there's a
Controllers
,Models
andViews
folder (just like a normal MVC project) and under theViews
folder, there's again aUserAdministration
folder which contains the views for the area.Also something else which is very important for the embedded views to work: you have to register a new view engine in the
Application_Start
method of yourglobal.asax.cs
file, did you do that?And... In your registration class, make sure you override the
RegisterArea
method which takes 2 parameters (AreaRegistrationContext context
andIApplicationBus bus
), and call the base implementation in there:If you don't call the base implementation, you have to at least add a
To make sure that your embedded views and resources are registered.
我按照 Fretje 的答案中的说明进行操作,然后在您的网站中添加对 EmbeddedResourceVirtualPathProvider 的 nuget 包引用,从而实现了此功能。
I got this working by following the instructions in Fretje's answer and then also add a nuget package reference to EmbeddedResourceVirtualPathProvider in your website.
您是否确定将视图标记为可移植区域中的嵌入资源?
我还发现可移植区域的一个很好的功能是您可以覆盖嵌入式视图,因此如果您在主机应用程序中放置一个具有与嵌入式视图相同的名称和位置但代码逻辑不同的视图,它将优先于嵌入式视图,很好!!!
希望这有帮助
Did you make sure that you Marked your View as Embedded Resource in your Portable Area?
Also i found that nice feature of portable areas is that you can override the embedded Views, so if you Place a View in your Host App with Same name and Location of the Embedded one with Different Code Logic it will take priority over the Embedded one Nice !!!
Hope this Helps