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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

ASP.NET MVC 3上的自定義錯(cuò)誤頁(yè)

ASP.NET MVC 3上的自定義錯(cuò)誤頁(yè)

胡說(shuō)叔叔 2019-07-05 10:06:48
ASP.NET MVC 3上的自定義錯(cuò)誤頁(yè)我正在開發(fā)一個(gè)MVC 3基礎(chǔ)網(wǎng)站,我正在尋找一個(gè)處理錯(cuò)誤的解決方案,并為每種錯(cuò)誤呈現(xiàn)自定義視圖。因此,假設(shè)我有一個(gè)“Error”Controller,其中他的主要操作是“Index”(泛型錯(cuò)誤頁(yè)),并且對(duì)于用戶可能出現(xiàn)的錯(cuò)誤,這個(gè)控制器還會(huì)有幾個(gè)操作,比如“Handle 500”或“HandleActionNotFound”。因此,網(wǎng)站上可能發(fā)生的每一個(gè)錯(cuò)誤都可能由這個(gè)“Error”Controller(例如:“Controller”或“Action”未找到、500、404、dbException等)處理。我使用站點(diǎn)地圖文件來(lái)定義網(wǎng)站路徑(而不是路由)。這個(gè)問(wèn)題已經(jīng)回答了,這是對(duì)Gweebz的答復(fù)。我的最后一個(gè)應(yīng)用程序錯(cuò)誤方法如下:protected void Application_Error() { //while my project is running in debug mode if (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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

下面是我如何處理自定義錯(cuò)誤的一個(gè)例子。我定義ErrorsController對(duì)于處理不同HTTP錯(cuò)誤的操作:

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_Error在……里面Global.asax并調(diào)用此控制器:

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);}


查看完整回答
反對(duì) 回復(fù) 2019-07-05
?
慕田峪9158850

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

您也可以在Web.Config文件中這樣做。下面是一個(gè)在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>


查看完整回答
反對(duì) 回復(fù) 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 645 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)