MVC 独立区域项目 - 无法找到视图
MVC Separate Area Project - Unable to find views
我正在为我的主 MVC 项目中的区域设置单独的项目。我按照这里的指南操作:
我在启动期间设置了断点,可以看到正在命中 RazorGeneratorMvcStart 以及设置路由的 AreaRegistration。 RazorGenerator.Mvc 已安装,我的 cshtml 页面上的自定义工具已设置为使用它。当我在启动我的主项目后访问 Area URL 时,我可以看到它点击了单独 Area 项目中的控制器,但是,我无法让它找到视图。我得到以下大量位置列表:
[InvalidOperationException: The view 'Index' or its master was not
found or no view engine supports the searched locations. The following
locations were searched:
STARAreaRegistration.cs
using System.Web.Mvc;
namespace AreaSTAR.Areas.STAR
{
public class STARAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "STAR";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"STAR_default",
"STAR/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
RazorGeneratorMvcStart.cs
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using RazorGenerator.Mvc;
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(AreaSTAR.RazorGeneratorMvcStart), "Start")]
namespace AreaSTAR {
public static class RazorGeneratorMvcStart {
public static void Start() {
var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {
UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
};
ViewEngines.Engines.Insert(0, engine);
// StartPage lookups are done by WebPages.
VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
}
}
}
Areas/STAR/Controllers/DefaultController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AreaSTAR.Areas.STAR.Controllers
{
public class DefaultController : Controller
{
// GET: STAR/Default
public ActionResult Index()
{
return View();
}
}
}
Areas/STAR/Views/Default/Index.cshtml:
@* Generator: MvcView *@
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>View1</title>
</head>
<body>
<div>
Index view
</div>
</body>
</html>
URL 在看到未找到视图错误时访问:http://localhost:53992/STAR/Default/Index
这是因为我不小心安装了 RazorEngine.Generator 扩展并将自定义工具设置为 RazorEngineGenerator,而不是安装 Razer Generator 扩展并将自定义工具设置为 RazorGenerator。
我正在为我的主 MVC 项目中的区域设置单独的项目。我按照这里的指南操作:
我在启动期间设置了断点,可以看到正在命中 RazorGeneratorMvcStart 以及设置路由的 AreaRegistration。 RazorGenerator.Mvc 已安装,我的 cshtml 页面上的自定义工具已设置为使用它。当我在启动我的主项目后访问 Area URL 时,我可以看到它点击了单独 Area 项目中的控制器,但是,我无法让它找到视图。我得到以下大量位置列表:
[InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
STARAreaRegistration.cs
using System.Web.Mvc;
namespace AreaSTAR.Areas.STAR
{
public class STARAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "STAR";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"STAR_default",
"STAR/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
RazorGeneratorMvcStart.cs
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using RazorGenerator.Mvc;
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(AreaSTAR.RazorGeneratorMvcStart), "Start")]
namespace AreaSTAR {
public static class RazorGeneratorMvcStart {
public static void Start() {
var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {
UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
};
ViewEngines.Engines.Insert(0, engine);
// StartPage lookups are done by WebPages.
VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
}
}
}
Areas/STAR/Controllers/DefaultController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AreaSTAR.Areas.STAR.Controllers
{
public class DefaultController : Controller
{
// GET: STAR/Default
public ActionResult Index()
{
return View();
}
}
}
Areas/STAR/Views/Default/Index.cshtml:
@* Generator: MvcView *@
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>View1</title>
</head>
<body>
<div>
Index view
</div>
</body>
</html>
URL 在看到未找到视图错误时访问:http://localhost:53992/STAR/Default/Index
这是因为我不小心安装了 RazorEngine.Generator 扩展并将自定义工具设置为 RazorEngineGenerator,而不是安装 Razer Generator 扩展并将自定义工具设置为 RazorGenerator。