Last active
April 15, 2017 09:18
-
-
Save 06b/b38f2d4bb7403688e628 to your computer and use it in GitHub Desktop.
ASP.NET MVC CustomErrors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewBag.Title = "Error"; | |
} | |
<h1>Looks like something went wrong.</h1> | |
<p>Sorry, there was an error in your request.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ErrorController : Controller | |
{ | |
// GET: Error | |
public ActionResult Index() | |
{ | |
//Deal with aspxerrorpath | |
if (!string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) | |
{ | |
return RedirectToAction("Index"); | |
} | |
else | |
{ | |
return View(); | |
} | |
} | |
// GET: PageNotFound | |
public ActionResult PageNotFound() | |
{ | |
//Deal with aspxerrorpath | |
if (!string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) | |
{ | |
return RedirectToAction("PageNotFound"); | |
} | |
else | |
{ | |
//Setting the Response.StatusCode to 404 causes a conflict on Azure | |
//Response.StatusCode = 404; //you may want to set this to 200 | |
return View(); | |
} | |
} | |
// GET: BadRequest | |
public ActionResult BadRequest() | |
{ | |
//Deal with aspxerrorpath | |
if (!string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) | |
{ | |
return RedirectToAction("BadRequest"); | |
} | |
else | |
{ | |
// Fix to deal with Bad Requests. | |
// The requested outcome would is that if a 400 error was thrown it wouldn't display the YSOD but instead a friendly error page. | |
// Which adding existingResponse="Replace" will handle which is good for when dealing with exceptions thrown due to a "potentially dangerous request value". | |
// However if the code returns a 400 status code for example which would be picked up in a json call - instead of returning the 400 and letting it get handled by the client, | |
// it get's hijack the response and throw an error page which results in a status code of 200 which means the client views it as a success. | |
// By setting the response code here it will allow to status code to be returned to the client and still serve the friendly error page. | |
Response.StatusCode = 400; | |
Response.StatusDescription = "Bad Request"; | |
//In some cases you may need to uncomment the line below with TrySkipIisCustomErrors | |
//Response.TrySkipIisCustomErrors = true; | |
return View(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewBag.Title = "Error"; | |
} | |
<h1>Looks like something went wrong.</h1> | |
<p>Sorry, we are experiencing technical difficulties. We are working on getting this problem resolved. Sorry for any inconvenience!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewBag.Title = "Page Not Found"; | |
} | |
<h1>Page Not Found</h1> | |
<p>The page that you have requested doesn’t exist or has been removed from our Web site. We’ve recently updated our site so the page you’re looking for might have been removed.</p> | |
<p>If you entered the Web page address yourself, please make sure that you entered the correct spelling.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration> | |
<system.web> | |
<customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRedirect"> | |
<error statusCode="404" redirect="~/Error/PageNotFound" /> | |
</customErrors> | |
</system.web> | |
<system.webServer> | |
<httpErrors errorMode="Custom" existingResponse="Replace"> | |
<clear /> | |
<error statusCode="400" responseMode="ExecuteURL" path="/Error/BadRequest"/> | |
<error statusCode="404" responseMode="ExecuteURL" path="/Error/PageNotFound"/> | |
<error statusCode="500" responseMode="ExecuteURL" path="/Error"/> | |
</httpErrors> | |
</system.webServer> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment