ASP.NET MVC 3、Razor 视图和可移植区域

发布于 2024-11-02 03:38:20 字数 259 浏览 0 评论 0原文

我正在尝试将可移植视图与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯℡ 2024-11-09 03:38:20

我已经在这个特定问题上苦苦挣扎了一段时间,但我想我终于弄清楚了。

文件夹结构以及项目内命名空间的调用方式对于其正常工作非常重要!

我在这里有一个带有嵌入式剃刀视图的便携式区域的工作示例:

https://github.com/fretje/MembershipStarterKit

看一下项目的结构。

该区域的名称为 UserAdministration,项目根目录中有一个 UserAdministrationRegistration 类,该类位于 UserAdministration 命名空间中。
然后在 Views 文件夹下有一个 ControllersModelsViews 文件夹(就像普通的 MVC 项目一样) ,还有一个 UserAdministration 文件夹,其中包含该区域的视图。

还有一些对于嵌入式视图的工作非常重要的事情:您必须在 global.asax.cs 文件的 Application_Start 方法中注册一个新的视图引擎,你这样做吗?

PortableAreaRegistration.RegisterEmbeddedViewEngine();

并且...在您的注册类中,请确保重写带有 2 个参数(AreaRegistrationContext contextIApplicationBus 总线)的 RegisterArea 方法,并且在那里调用基本实现:

public override void RegisterArea(AreaRegistrationContext context, 
    IApplicationBus bus)
{
    base.RegisterArea(context, bus); // <== very important!

    context.MapRoute(
        "UserAdministration", 
        AreaName + "/{controller}/{action}/{id}",
        new { controller = "UserAdministration", action = "Index", 
              id = UrlParameter.Optional }
    );
}

如果您不调用基本实现,则必须至少添加一个

RegisterAreaEmbeddedResources();

以确保您的嵌入视图和资源已注册。

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:

https://github.com/fretje/MembershipStarterKit

Take a look at the structure of the project.

The area's name is UserAdministration, and there is a UserAdministrationRegistration class in the root of the project, which resides in the UserAdministration namespace.
Then there's a Controllers, Models and Views folder (just like a normal MVC project) and under the Views folder, there's again a UserAdministration 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 your global.asax.cs file, did you do that?

PortableAreaRegistration.RegisterEmbeddedViewEngine();

And... In your registration class, make sure you override the RegisterArea method which takes 2 parameters (AreaRegistrationContext context and IApplicationBus bus), and call the base implementation in there:

public override void RegisterArea(AreaRegistrationContext context, 
    IApplicationBus bus)
{
    base.RegisterArea(context, bus); // <== very important!

    context.MapRoute(
        "UserAdministration", 
        AreaName + "/{controller}/{action}/{id}",
        new { controller = "UserAdministration", action = "Index", 
              id = UrlParameter.Optional }
    );
}

If you don't call the base implementation, you have to at least add a

RegisterAreaEmbeddedResources();

To make sure that your embedded views and resources are registered.

小鸟爱天空丶 2024-11-09 03:38:20

我按照 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.

野味少女 2024-11-09 03:38:20

您是否确定将视图标记为可移植区域中的嵌入资源?

我还发现可移植区域的一个很好的功能是您可以覆盖嵌入式视图,因此如果您在主机应用程序中放置一个具有与嵌入式视图相同的名称和位置但代码逻辑不同的视图,它将优先于嵌入式视图,很好!!!

希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文