【发布时间】:2016-08-25 21:50:54
【问题描述】:
我正在为我的主 MVC 项目中的区域设置单独的项目。我按照这里的指南进行操作:
https://stackoverflow.com/a/12912161/155110
我在启动期间设置了断点,可以看到 RazorGeneratorMvcStart 以及设置路由的 AreaRegistration 正在被命中。 RazorGenerator.Mvc 已安装,并且我的 cshtml 页面上的自定义工具已设置为使用它。当我在启动我的主项目后访问区域 URL 时,我可以看到它到达了单独的区域项目中的控制器,但是,我无法让它找到视图。我得到以下位置列表:
[InvalidOperationException:视图“索引”或其主视图不是 找到或没有视图引擎支持搜索的位置。以下 已搜索位置:
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
【问题讨论】:
标签: asp.net-mvc razor asp.net-mvc-areas