MVC4托管来自其他MVC项目的视图

时间:2013-04-26 16:25:33

标签: asp.net razor asp.net-mvc-4 razorgenerator

我试图弄清楚如何托管在不同解决方案下构建的MVC4应用程序。有许多使用带有区域的RazorGenerator nuget包在SO和Web上执行此操作的示例 - 它非常有效。在我的情况下,我想避免使用区域 - 我公司开发的每个应用程序都将在它自己的MVC4项目中(然后集中在同一个解决方案中)。

我已将RazorGenerator集成到我的应用程序中,代码生成按预期工作。但是,我的主机解决方案无法在其默认位置找到View。例如,我在一个名为MyAccount / Index的应用程序中内置了一个Controller / View。

控制器:

namespace Accounts.Controllers
{
    public class MyAccountController : Controller
    {
        //
        // GET: /MyAccount/
        public ActionResult Index()
        {
            return View();
        }
    }
}

查看(由RazorGenerator生成):

namespace Accounts.Views.MyAccount
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Web.Mvc.Html;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "1.5.4.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/Views/MyAccount/Index.cshtml")]
    public partial class Index : System.Web.Mvc.WebViewPage<dynamic>
    {
        public Index()
        {
        }
        public override void Execute()
        {

            #line 1 "..\..\Views\MyAccount\Index.cshtml"

    ViewBag.Title = "Index";


            #line default
            #line hidden
WriteLiteral("\r\n\r\n<h2>Index</h2>\r\n\r\nMy AccountController Index view.");

        }
    }
}

我知道默认情况下,ViewEngines会尝试在默认位置(Views和Shared)中查找此视图,因此我将自己的ViewEngine添加到Engines集合中:

MyViewEngine.cs:

 public class MyViewEngine : RazorViewEngine
    {
        private static string[] _viewLocations 
           = new string[]
             {
                "~/Accounts/Views/{1}/{0}.cshtml"
             };

        public MyViewEngine()
        {
            base.ViewLocationFormats = ViewLocationFormats.Union(_viewLocations).ToArray();
        }
    }

但是,仍未找到该视图:

The view 'Index' or its master was not found or no view engine supports the searched locations. 
The following locations were searched: 
~/Views/MyAccount/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/MyAccount/Index.aspx
~/Views/MyAccount/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/MyAccount/Index.vbhtml 
~/Views/Shared/Index.vbhtml
~/Accounts/Views/MyAccount/Index.cshtml

也许我误解了视图的位置 - 我以为它会在Accounts / Views / MyAccount /中找到。我可能做错了什么想法?

谢谢!

1 个答案:

答案 0 :(得分:1)

发现我的问题 - 原因是没有使用RazorGeneratorMvcStart预热代码。添加nuget包时,它会自动生成到App_Start文件夹中,但是我错误地删除了它。