共享控制器和视图与多个Web应用程序 [英] Sharing Controllers and Views with Multiple Web Applications

查看:129
本文介绍了共享控制器和视图与多个Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的控制器和视图突破到单独的类库,使他们能够在多个ASP.NET MVC 3应用程序中重用。使用一个单独的程序的时候,但是得到视图引擎来查找视图控制器部分是不是一个问题了。

I would like to break out my controllers and views into separate class libraries so they can be reused in multiple ASP.NET MVC 3 applications. The controllers part was not an issue when using a separate assembly, however getting the view engine to locate the view was.

我最终使用<一个href=\"http://www.chrisvandesteeg.nl/2010/11/22/embedding-$p$p-compiled-razor-views-in-your-dll/\">Compile您的asp.net的MVC剃刀意见纳入一个单独的DLL 。

有没有一种更简单的方法,我错过了?

Is there an easier way that I missed?

推荐答案

我已经修改的想法发布的此处,与MVC3工作。这是pretty快速和容易。唯一的小缺点是共享的观点需要嵌入资源,因此,编

I have modified the idea posted here, to work with MVC3. It was pretty fast and easy. The only minor drawback is that shared views need to be embedded resources, and therefore, compiled.


  • 把你的共享视图(.cshtml,.vbhtml文件)到库项目​​。 (我也有一些共享的控制器,在这个项目中。)如果你想从你的应用程序中使用_Layout.cshtml,请确保您包括_ViewStart.cshtml,指向它,与你共同意见。

  • Put your shared views (.cshtml, .vbhtml files) into a library project. (I also have some shared controllers in this project.) If you want to use the _Layout.cshtml from your application, make sure you include a _ViewStart.cshtml, that points to it, in with your shared views.

在库项目,将所有的意见生成操作属性,以嵌入的资源的。

In the library project, set all of your views' Build Action properties to Embedded Resource.

在库项目中添加以下code,将你的意见的内容写入到TMP / views目录。

In the library project add the following code which will write the contents of your views to a tmp/Views directory.

public class EmbeddedResourceViewEngine : RazorViewEngine
{
    public EmbeddedResourceViewEngine()
    {
        ViewLocationFormats = new[] {
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx",
        "~/Views/{1}/{0}.cshtml",
        "~/Views/{1}/{0}.vbhtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Shared/{0}.vbhtml",
        "~/tmp/Views/{0}.cshtml",
        "~/tmp/Views/{0}.vbhtml"
    };
        PartialViewLocationFormats = ViewLocationFormats;

        DumpOutViews();
    }

    private static void DumpOutViews()
    {
        IEnumerable<string> resources = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceNames().Where(name => name.EndsWith(".cshtml"));
        foreach (string res in resources) { DumpOutView(res); }
    }

    private static void DumpOutView(string res)
    {
        string rootPath = HttpContext.Current.Server.MapPath("~/tmp/Views/");
        if (!Directory.Exists(rootPath))
        {
            Directory.CreateDirectory(rootPath);
        }

        Stream resStream = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceStream(res);
        int lastSeparatorIdx = res.LastIndexOf('.');
        string extension = res.Substring(lastSeparatorIdx + 1);
        res = res.Substring(0, lastSeparatorIdx);
        lastSeparatorIdx = res.LastIndexOf('.');
        string fileName = res.Substring(lastSeparatorIdx + 1);

        Util.SaveStreamToFile(rootPath + fileName + "." + extension, resStream);
    }
}

我使用阿德里安的StreamToFile作家,发现这里


  • 在您的应用程序的Global.asax.cs中添加:

public static void RegisterCustomViewEngines(ViewEngineCollection viewEngines)
{
   //viewEngines.Clear(); //This seemed like a bad idea to me.
   viewEngines.Add(new EmbeddedResourceViewEngine());
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    RegisterCustomViewEngines(ViewEngines.Engines);
}

这篇关于共享控制器和视图与多个Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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