404 error trong MVC

1. Webconfig.
2. ErrorController.cs
3. View>Error> Notfound.cshtml và Unauthorized.cshtml
Source: https://github.com/chuchuva/Test404

Chi tiết:
1. Webconfig:
a) Thêm dòng này:
<customErrors mode="On" />

b) Thêm vào:
<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
      <remove statusCode="500" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Unauthorized" />
    </httpErrors>
</system.webServer>

2. ErrorController.cs

        public ActionResult NotFound()
        {
            Response.StatusCode = 404;
            return View();
        }
        public ActionResult Unauthorized()
        {
            Response.StatusCode = 500;
            return View();
        }

3. View>
@{
    ViewBag.Title = "Error 404";
}

<center style="background: #ffffff">
    <img src="@Url.Content("~/Content/img/nv.jpg")" />
    <br />
    <div class="text404">
        <div class="oops">Oops!</div><p>
            <div class="rowdescription">Có lẽ nội dung bạn đang tìm quá hay nên đã bị đánh cắp.</div>
        <p>
            <div class="clickhere">Bấm <a href="http://abc.com/"> vào đây </a> để trở về trang chủ</div>
        <p>
            <div style="font-size: 10pt"><a href="http://abc.com/"> http://abc.com/ </a></div>
    </div>
</center>
<script type="text/javascript">
    setTimeout(function () {
        window.lochttps://github.com/chuchuva/Test404ation.href = "http://abc.com";
    }, 5000);
</script>

Source: https://github.com/chuchuva/Test404

Mình đã làm như thế này, có nhiều cách khác, hy vọng cách này cũng giúp được các bạn.

Nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com

MVC định dạng tiền tệ vnđ - Format currency vnd in MVC