Asp.Net Core 404處理


在使用Asp.Net Core Mvc時 404處理整理如下

一、自帶404狀態處理

1.控制器視圖子彈404視圖 NotFoundResult,NotFoundObjectResult

        //
        // 摘要:
        //     Creates an Microsoft.AspNetCore.Mvc.NotFoundObjectResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound
        //     response.
        //
        // 返回結果:
        //     The created Microsoft.AspNetCore.Mvc.NotFoundObjectResult for the response.
        [NonAction]
        public virtual NotFoundObjectResult NotFound(object value);
        //
        // 摘要:
        //     Creates an Microsoft.AspNetCore.Mvc.NotFoundResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound
        //     response.
        //
        // 返回結果:
        //     The created Microsoft.AspNetCore.Mvc.NotFoundResult for the response.
        [NonAction]
        public virtual NotFoundResult NotFound();

2.當前操作返回404狀態,或者返回404的一句話提示。

 

二、自定義404頁面顯示

在網站中,為了增強提前,通常使用自定義404頁面

1.自定義404視圖,在控制器中返回

    /// <summary>
    /// 定義404視圖
    /// </summary>
    public class NotFoundViewResult : ViewResult
    {
        public NotFoundViewResult(string viewName)
        {
            ViewName = viewName;
            StatusCode = (int)HttpStatusCode.NotFound;
        }
    }

2.在控制器中返回使用

        public IActionResult Index()
        {
            //返回404頁面
            return new NotFoundViewResult("~/views/Error/code_404.cshtml");

            return View();
        }

3.呈現結果:

 

三、全站統一處理404 或者500的錯誤,並自定義頁面內容

 

1.使用app.UseStatusCodePagesWithReExecute(path,param)可以指定錯誤和參數

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseStatusCodePagesWithReExecute("/Home/Error", null);
            }

 

2. 頁面內容根據狀態處理

@{
    int code = this.Context.Response.StatusCode;
    ViewData["Title"] = code + "頁面訪問錯誤";
}
@section Head{

}
@section Footer{

}
<!-- 主體內容 -->
<div class="maincontent maincontentcoverage">
    <div style="height:40px;"></div>
    <!-- 主要內容 -->
    <div class="primarycoverage aboutpage" style="font-size:16px;">
        <div class="researchmain" style="margin-top: 0px;padding-bottom:60px;">
            <div class="aboutSD">
                @{
                    if (code == 404)
                    {
                        <div class="majorintroduct" style="margin-top: 0px;padding:50px;">
                            <div class="flowerTitle">
                                404了
                            </div>
                            <div class="introlduct" style="padding: 50px 50px;">
                                <div>
                                    您要訪問的頁面不存在或已經刪除~
                                </div>
                                <p></p>
                                <p></p>
                                <p></p>
                                <p></p>
                                <div class="button videoBtn" style="font-size:14px;" href="/">點擊返回首頁</div>
                            </div>
                        </div>
                    }
                    else
                    {
                        <div class="majorintroduct" style="margin-top: 0px;padding:50px;">
                            <div class="flowerTitle">
                                ==> 500 訪問出錯
                            </div>
                            <div class="introlduct" style="padding: 50px 50px;">
                                <div>訪問出錯,請點擊下邊按鈕返回。</div>
                                <p></p>
                                <p></p>
                                <p></p>
                                <p></p>
                                <div class="button videoBtn" style="font-size:14px;" href="/">點擊返回首頁</div>
                            </div>
                        </div>
                    }
                }
            </div>
        </div>
    </div>
</div>

顯示效果:

 

更多:

Asp.Net Core異常處理整理

.Net Core郵件發送之MailKit

Asp.Net Core中Json序列化處理整理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM