.NET MVC ページで 独自の404ページを設置する
Web.config の「system.web」の下に 404 エラー時に /Error/NotFound へ移動するよう設定します
Web.config
<system.web>
<!--独自の404ページ-->
<customErrors defaultRedirect="~/Error/" mode="On">
<error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>
<!--独自の404ページ-->
ネームスペースの「MYAPP」のところは適宜書き換えてください。 NotFound のビューを返すようにしてあります
/Controllers/ErrorController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MYAPP.Controllers
{
public class ErrorController : Controller
{
// GET: Error
public ActionResult Index()
{
return View();
}
// GET: Error/NotFound
public ActionResult NotFound()
{
ViewBag.Title = "Error 404";
return View( "NotFound" );
}
}
}
お好きなhtmlを作成してください。
/Views/Error/NotFound.cshtml
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>404 Not Found.</h1>
</body>
</html>