我創建了一個自定義AuthorizeAttribute
,如果用戶無權查看請求的控制器,則會將其發送到錯誤頁面。MVC Not Loading完整視圖
重定向他們是如下代碼:
Protected Overrides Sub HandleUnauthorizedRequest(filterContext As AuthorizationContext)
Dim result = New ViewResult()
result.ViewName = "Error"
result.ViewBag.ErrorMessage = "oops, you are not allowed"
filterContext.Result = result
End Sub
據其重定向到~/Shared/Error.vbhtml
,就像它應該;但是,頁面只加載我的佈局,並且包含標題"Error"
,但不會加載錯誤頁面的任何內容。是
錯誤頁面的內容如下:
@ModelType System.Web.Mvc.HandleErrorInfo
@Code
ViewData("Title") = "Error"
End Code
@ViewBag.ErrorMessage
<h2>
Sorry, an error occurred while processing your request.
</h2>
爲什麼沒有加載「對不起,發生了錯誤...」的消息,我怎麼可以把它做呢?謝謝。
你可能想看看[這裏](http://stackoverflow.com/問題/ 5070339/custom-authorize-attribute)和[this](http://stackoverflow.com/questions/4664302/mvc-roleprovider-and-authorize-attribute)鏈接 – Yasser 2012-08-07 17:41:49
你確定'〜/ Views/Shared/Error.vbhtml'視圖正在呈現?有沒有其他的觀點可以使用?像'Error.cshtml'一樣? – 2012-08-07 17:43:24
@Darin:是的,因爲當我將result.ViewName =「Error」更改爲result.ViewName =「Error1」時,它給我一個錯誤。 – user1477388 2012-08-07 17:44:22