Reputation: 1966
Not sure if I am missing something obvious, or if this could be a bug.
In startup.cs, method Configure, I have `UseStatusCodePagesWithRedirects'. I ended up putting a static file, hoping it would make a difference. Before I used the error method in Home controller, but the end result was identical.
Here is my Configure method in full:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStatusCodePagesWithRedirects("~/errors/{0}.html");
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
I have an error
directory, with a single static 404.html file.
if I create on purpose a 404 error, typing "joe" for example after "Home" in the URL, I get
The localhost page isn’t working
localhost redirected you too many times.
Checking with F12/Network in Edge, I see it is redirected to the proper page, but way too many times.
So, I guess my question is: how do I change the response code once the proper error page has been hit? But I feel I should not have to do this.
Thanks in advance for helping me to understand what is happening here.
By the way, if I simply put app.UseStatusCodePages();
it does work, but that is pretty ugly....
Upvotes: 1
Views: 12027
Reputation: 172
Following what is said in this web-site:
you must put inside your Startup.cs->Configure must put this:
// Will give error status code
//app.UseStatusCodePages();
//app.UseStatusCodePagesWithRedirects("/Error/{0}");
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
Then you must put the following code inside you Home Controller:
public IActionResult Error(int? statusCode = null)
{
if (statusCode.HasValue)
{
if (statusCode == 404 || statusCode == 500)
{
var viewName = statusCode.ToString();
return View(viewName);
}
}
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
And finally you will add 2 new Views to your Views->Home folder, the 404.cshtml and 500.cshtml
Where inside of 404.cshtml you can put something like:
@{
ViewBag.Title = "Page Not Found" ;
}
<h1>(404) Page Not Found.</h1>
<p>This page doesn't exist.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>
Inside of 500.cshtml:
@{
ViewBag.Title = "(500) An Error Occurred";
}
<h1>(500) An Error Occurred.</h1>
<p>Something went wrong.</p>
<p>Please, contact the support by emailing them this page error.</p>
<p>To find the available contacts go to: @Html.ActionLink("Contacts", "Contact", "Home")</p>
Any other error executes the default Error.cshtml template (i'm using the Microsoft default Error.cshtml).
Upvotes: 2