[英]How can I redirect to an error page in asp.net mvc 3?
因此,我正在嘗試使用以下命令重定向到我自己的AuthorizeAttribute中的錯誤頁面:
filterContext.Result = new HttpStatusCodeResult(403);
在我的web.config中,最初我嘗試過:
<customErrors defaultRedirect="/Error" mode="On">
<error statusCode="403" redirect="/UnAuthorize" />
</customErrors>
經過我的嘗試:
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
之后,我創建一個ErrorController並嘗試了該操作:
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Error/UnAuthorize" />
</customErrors>
但是瀏覽器仍然顯示403的默認錯誤頁面,知道嗎?
可能的解決方案:嗯,我看到了@bobek和@Robert Levy的答案,但我發現了另一種方法,簡單了一點。
在我的AuthorizeAttribute中,我創建了一個名為RedirectOnErrorTo的屬性,並且在我的AuthorizeAttribute的OnAuthorization方法中,我做到了:
if (!string.IsNullOrEmpty(this.RedirectOnErrorTo))
{
filterContext.Result = new RedirectResult(this.RedirectOnErrorTo);
}
因此,現在當我聲明此屬性時,我選擇要重定向的路徑。
它不像我只想使用web.config那樣自動化,但是變得很有用。 您如何看待,這是解決此問題的好方法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.