第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

asp.net MVC3上的自定義錯誤頁面

asp.net MVC3上的自定義錯誤頁面

asp.net MVC3上的自定義錯誤頁面我正在開發(fā)一個MVC3基礎網站,我正在尋找一個處理錯誤的解決方案,并為每種錯誤渲染自定義視圖。因此,假設我有一個“錯誤”控制器,其主要操作是“索引”(通用錯誤頁面),并且此控制器將針對用戶可能出現(xiàn)的錯誤(如“Handle500”或“HandleActionNotFound”)執(zhí)行更多操作。因此,網站上可能發(fā)生的每個錯誤都可能由此“錯誤”控制器處理(例如:“Controller”或“Action”未找到,500,404,dbException等)。我使用Sitemap文件來定義網站路徑(而不是路由)。這個問題已經回答了,這是對Gweebz的回復我的最終applicaiton_error方法如下:protected void Application_Error() {//while my project is running in debug modeif (HttpContext.Current.IsDebuggingEnabled && WebConfigurationManager.AppSettings["EnableCustomErrorPage"].Equals("false")){     Log.Logger.Error("unhandled exception: ", Server.GetLastError());}else{     try     {         var exception = Server.GetLastError();         Log.Logger.Error("unhandled exception: ", exception);         Response.Clear();         Server.ClearError();         var routeData = new RouteData();         routeData.Values["controller"] = "Errors";         routeData.Values["action"] = "General";         routeData.Values["exception"] = exception;         IController errorsController = new ErrorsController();         var rc = new RequestContext(new HttpContextWrapper(Context), routeData);         errorsController.Execute(rc);     }     catch (Exception e)     {         //if Error controller failed for same reason, we will display static HTML error page         Log.Logger.Fatal("failed to display error page, fallback to HTML error: ", e);         Response.TransmitFile("~/error.html");     }}}
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

這是我如何處理自定義錯誤的示例。我定義了一個ErrorsController處理不同HTTP錯誤的動作:

public class ErrorsController : Controller{
    public ActionResult General(Exception exception)
    {
        return Content("General failure", "text/plain");
    }

    public ActionResult Http404()
    {
        return Content("Not found", "text/plain");
    }

    public ActionResult Http403()
    {
        return Content("Forbidden", "text/plain");
    }}

然后我訂閱了Application_Errorin Global.asax并調用了這個控制器:

protected void Application_Error(){
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;
    Response.Clear();
    Server.ClearError();
    var routeData = new RouteData();
    routeData.Values["controller"] = "Errors";
    routeData.Values["action"] = "General";
    routeData.Values["exception"] = exception;
    Response.StatusCode = 500;
    if (httpException != null)
    {
        Response.StatusCode = httpException.GetHttpCode();
        switch (Response.StatusCode)
        {
            case 403:
                routeData.Values["action"] = "Http403";
                break;
            case 404:
                routeData.Values["action"] = "Http404";
                break;
        }
    }

    IController errorsController = new ErrorsController();
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    errorsController.Execute(rc);}


查看完整回答
反對 回復 2019-08-09
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

您也可以在Web.Config文件中執(zhí)行此操作。這是一個適用于IIS 7.5的示例。

     <system.webServer>
          <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File">
                <remove statusCode="502" subStatusCode="-1" />
                <remove statusCode="501" subStatusCode="-1" />
                <remove statusCode="412" subStatusCode="-1" />
                <remove statusCode="406" subStatusCode="-1" />
                <remove statusCode="405" subStatusCode="-1" />
                <remove statusCode="404" subStatusCode="-1" />
                <remove statusCode="403" subStatusCode="-1" />
                <remove statusCode="401" subStatusCode="-1" />
                <remove statusCode="500" subStatusCode="-1" />
                <error statusCode="500" path="/notfound.html" responseMode="ExecuteURL" />
                <error statusCode="401" prefixLanguageFilePath="" path="/500.html" responseMode="ExecuteURL" />
                <error statusCode="403" prefixLanguageFilePath="" path="/403.html" responseMode="ExecuteURL" />
                <error statusCode="404" prefixLanguageFilePath="" path="/404.html" responseMode="ExecuteURL" />
                <error statusCode="405" prefixLanguageFilePath="" path="/405.html" responseMode="ExecuteURL" />
                <error statusCode="406" prefixLanguageFilePath="" path="/406.html" responseMode="ExecuteURL" />
                <error statusCode="412" prefixLanguageFilePath="" path="/412.html" responseMode="ExecuteURL" />
                <error statusCode="501" prefixLanguageFilePath="" path="/501.html" responseMode="ExecuteURL" />
                <error statusCode="502" prefixLanguageFilePath="" path="/genericerror.html" responseMode="ExecuteURL" />
           </httpErrors></system.webServer>


查看完整回答
反對 回復 2019-08-09
  • 3 回答
  • 0 關注
  • 572 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號