ASP.NET MVC 3,剃刀视图和便携式领域 [英] ASP.NET MVC 3, Razor Views, and Portable Areas

查看:111
本文介绍了ASP.NET MVC 3,剃刀视图和便携式领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用随身携带的意见与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?

推荐答案

我一直在挣扎了一会儿这个特定的问题,但我想我终于想通了。

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:

<一个href=\"https://github.com/fretje/MembershipStarterKit\">https://github.com/fretje/MembershipStarterKit

看看该项目的结构。

该地区的名称是 UserAdministration ,并且在该项目的根,它位于一个 UserAdministrationRegistration 类在 UserAdministration 命名空间。
然后有一个控制器模式查看文件夹(只像一个正常的MVC项目),并在查看文件夹,里面又是其中包含了区域意见的 UserAdministration 文件夹

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.

还有别的东西是嵌入式的意见很重要的工作:你必须注册在的的Application_Start 法新视图引擎你的的global.asax.cs 文件,你做了吗?

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();

和...在你的注册类,请确保您覆盖 RegisterArea 方法,需要2个参数( AreaRegistrationContext背景 IApplicationBus总线),并调用基实现在那里:

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.

这篇关于ASP.NET MVC 3,剃刀视图和便携式领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆