【发布时间】:2016-02-23 03:50:33
【问题描述】:
我正在尝试在自定义ActionFilterAttribute 中设置布局路径,我编写如下:
public class LayoutInjecterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = "~/Views/Layouts/Test.cshtml"
}
}
}
这里的 Test.cshtml 是不同项目中的预编译视图(在 RazorGenerator 的帮助下)。
但它给了我错误:
未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置: ~/Views/Home/Index.cshtml ~/Views/Shared/Index.cshtml ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Layouts/Test.cshtml
控制器其实很简单:
[LayoutInjecter]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
【问题讨论】:
-
您是否创建了控制器布局?
-
不,没有任何名为 'Layouts' 的控制器
-
请添加一个名为 layouts 的新控制器,然后创建一个名为 Test 的新操作结果并返回视图,这应该可以工作
-
所有项目中都有 Layouts 文件夹吗?
-
没有。只有库项目具有该文件夹。我试图向 mvc 项目添加一个空文件夹,但没有任何改变:(
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razorgenerator