How do I achieve the below functionality?
My controler:
if (something == null) { //return the view with 404 http header return View(); } //return the view with 200 http header return View();
A simple solution is to check for the HTTP status code 404 in the response. If found, you can redirect the control to a page that exists. The following code snippet illustrates how you can write the necessary code in the Configure method of the Startup class to redirect to the home page if a 404 error has occurred.
It is important to use the 404 status code and not a 3xx code. @Shekhar_Pro: According to iis.net/ConfigReference/system.webServer/httpErrors/error if you use the ExecuteURL responseMode the path must be "a server relative URL", that's why I used /http404. aspx , in your case try /error.
The simplest and easiest way to fix your 404 error code is to redirect the page to another one. You can perform this task using a 301 redirect. What's 301, you may ask? It's a redirect response code that signals a browser that the content has been transferred to another URL.
Just write
Response.StatusCode = 404;
before returning the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With