Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return 404 with asp.net mvc view

Tags:

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(); 
like image 237
user1615362 Avatar asked Dec 07 '12 14:12

user1615362


People also ask

How to handle 404 error in ASP net MVC 5?

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.

How to redirect 404 error page in ASP net?

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.

How do I return a 404 Not Found 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.


1 Answers

Just write

Response.StatusCode = 404; 

before returning the view.

like image 97
fero Avatar answered Sep 21 '22 21:09

fero