I'm new to ASP.NET core, so I hope you bear with me on this one. This is similar to this unanswered question.
When I'm testing a brand new C# MVC project, and I enter a wrong URL, there's no info. Just a blank page.
To solve this, I have adapted startup.cs
adding a UseStatusCodePagesWithReExecute()
to return a static 404.html page. This works.
So far, so good.
Now, I am coding a basic login logic. For testing purposes, I'm calling return NotFound();
when the post parameters are missing. This doesn't return anything. I'm aware that 404 is not the correct response, but I have seen NotFound();
in the generated code and I think that's what's returning blank pages, so I want to solve that before moving on.
The app.UseDeveloperException();
doesn't seem to be called for this. I'm not sure how to test that.
Is there a way to override the NotFound();
behavior to get a 404.html somehow?
This is the Tutorial I have used to set up my project.
Edit:
Based on the comments of Alexander Powolozki, I have replaced NotFound();
with Redirect("~/404.html");
. This works.
// Wherever you want to return your standard 404 page
return Redirect("Home/StatusCode?code=404");
public class HomeController : Controller
{
// This method allows for other status codes as well
public IActionResult StatusCode(int? code)
{
// This method is invoked by Startup.cs >>> app.UseStatusCodePagesWithReExecute("/Home/StatusCode", "?code={0}");
if (code.HasValue)
{
// here is the trick
this.HttpContext.Response.StatusCode = code.Value;
}
//return a static file.
try
{
return File("~/" + code + ".html", "text/html");
}
catch (FileNotFoundException)
{
return Redirect("Home/StatusCode?code=404");
}
}
}