Asp.net-Mvc
使用 asp.net MVC 以程式方式重定向到 404 頁面
我創建了一個 Asp.net MVC 應用程序。現在需要 404 處理。
已更新 global.asax 並根據狀態碼顯示 404 頁面。還在 web.config 中添加了 customErrors 屬性。它工作正常。
現在,當任何不符合我們要求的東西時,我想以程式方式重定向到 404。
IE
if(!valid) { return RedirectToAction("Index", "Page404"); }它工作正常,但有兩種狀態,一種是 301,然後是 404。那麼我該如何防止 301?我只需要404。
我怎樣才能做到這一點?
在您的
web.config中,添加:<customErrors mode="On" > <error statusCode="404" redirect="/404.shtml" /> </customErrors>當找不到請求的資源時,這將重定向到 404.shtml 頁面。
**注意:**對於這種情況,無需以程式方式重定向使用者。
編輯: 我從字面上建議:
if (Context.Response.StatusCode == 404) { // handle this }
只需從您的操作中返回:
return HttpNotFound();