我從主MVC項目(我們稱之爲Main)創建了一個單獨項目中的佈局視圖(讓我們稱之爲Shared),然後使用RazorGenerator編譯該視圖。當我在主任何MVC頁面使用的佈局正常工作:使用ActionFilter將編譯視圖(RazorGenerator)設置爲佈局
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
然而,當我使用一個ActionFilter這樣設置佈局:
public class LayoutAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var viewResult = filterContext.Result as ViewResult;
viewResult.MasterName = "~/Views/Shared/_Layout.cshtml";
}
}
,只需在控制器中使用它主要是這樣的:
[Layout]
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
我得到這個錯誤:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/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/Shared/_Layout.cshtml
我真的很想使用內置到Shared項目中的ActionFilter,以便我可以在Main項目(或任何其他項目)中的任何控制器中使用它。
任何想法,爲什麼我得到這個錯誤?
@jamiedanq它在那裏。就像我說的,當我直接從索引視圖使用Layout =「...」時,它就可以工作。當我刪除該行並使用ActionFilter時,我得到錯誤。 –
錯過了那部分我的錯 – jamiedanq
LayoutAttribute類是否與'佈局' – jamiedanq