のweb.configのsystem.web
で
オフにカスタムエラーを
<system.web>
<customErrors mode="Off" />
</system.web>
system.webServerでhttpエラーを設定します。
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Auto">
<clear />
<error statusCode="404" responseMode="ExecuteURL" path="/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
</system.webServer>
ErrorContoller.cs
[AllowAnonymous]
public class ErrorController : Controller {
// GET: Error
public ActionResult NotFound() {
var statusCode = (int)System.Net.HttpStatusCode.NotFound;
Response.StatusCode = statusCode;
Response.TrySkipIisCustomErrors = true;
HttpContext.Response.StatusCode = statusCode;
HttpContext.Response.TrySkipIisCustomErrors = true;
return View();
}
public ActionResult Error() {
Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
Response.TrySkipIisCustomErrors = true;
return View();
}
}
のconfigureルートRouteConfig.cs
public static void RegisterRoutes(RouteCollection routes) {
//...other routes
routes.MapRoute(
name: "404-NotFound",
url: "NotFound",
defaults: new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
name: "500-Error",
url: "Error",
defaults: new { controller = "Error", action = "Error" }
);
//..other routes
//I also put a catch all mapping as last route
//Catch All InValid (NotFound) Routes
routes.MapRoute(
name: "NotFound",
url: "{*url}",
defaults: new { controller = "Error", action = "NotFound" }
);
}
そして最後には、あなたは、コントローラのアクションのためのビューを持っていることを確認し、これらの要求を処理するための単純なエラーコントローラを作成します。
Views/Shared/NotFound.cshtml
Views/Shared/Error.cshtml
追加のエラーがある場合は、そのパターンに従い、必要に応じて追加することができます。これにより、リダイレクトが回避され、発生した元のHTTPエラーステータスが維持されます。
http://stackoverflow.com/questions/39246370/how-to-pass-error-message-to-error-view-in-mvc-5/39248096#39248096に加えて、私はあなたの答えに従いました質問... 404が発生するとうまく動作しました – Gaurav123
他の例外の場合は、Error()アクションが発生して、私が思うように情報を送信しないので、エラーページのモデルがnullになります – Gaurav123
サーバの最終エラーをチェックし、そこにモデルを構築できるかどうかを確認してください – Nkosi