我试图弄清楚如何托管在不同解决方案下构建的 MVC4 应用程序。在 SO 和 Web 上有很多使用带有 Areas 的 RazorGenerator nuget 包执行此操作的示例 - 它工作得非常好。在我的情况下,我想避免使用区域 - 我公司开发的每个应用程序都将在它自己的 MVC4 项目中(然后共同在同一个解决方案中)。
我已将 RazorGenerator 集成到我的应用程序中,代码生成按预期工作。但是,我的主机解决方案无法在其默认位置找到视图。例如,我在一个名为 MyAccount/Index 的应用程序中内置了一个控制器/视图。
控制器:
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 会尝试在默认位置(视图和共享)中找到这个视图,所以我将自己的 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/ 中找到。有什么想法我可能做错了吗?
谢谢!