Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning 404 Error ASP.NET MVC 3

I have tried the following 2 things to have a page return a 404 error:

public ActionResult Index()
{
    return new HttpStatusCodeResult(404);
}

public ActionResult NotFound()
{
    return HttpNotFound();
}

but both of them just render a blank page. How can I manually return a 404 error from within ASP.NET MVC 3?

like image 349
ryanzec Avatar asked Apr 12 '11 12:04

ryanzec


People also ask

How to return 404 ASP net?

How to return 404 status code from a MVC application. First way is to instantiate HttpNotFoundResult class and return the object. Next alternative is to makes use of HttpNotFound() helper method of the Controller class which returns the HttpNotFoundResult instance.

How to handle 404 error in c#?

Fortunately you don't need to parse the customErrors section to get name of the custom 404 page. Just throw HttpException: throw new HttpException(404, "Page you requested is not found"); ASP.NET run-time will catch the exception and will redirect to the custom 404.

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.


5 Answers

If you inspect the response using fiddler, I believe you'll find that the blank page is in fact returning a 404 status code. The problem is no view is being rendered and thus the blank page.

You could get an actual view to be displayed instead by adding a customErrors element to your web.config that will redirect the user to a specific url when a certain status code occurs which you can then handle as you would with any url. Here's a walk-through below:

First throw the HttpException where applicable. When instantiating the exception, be sure to use one of the overloads which takes a http status code as a parameter like below.

throw new HttpException(404, "NotFound");

Then add an custom error handler in your web.config file so that you could determine what view should be rendered when the above exception occurs. Here's an example below:

<configuration>
    <system.web>
        <customErrors mode="On">
          <error statusCode="404" redirect="~/404"/>
        </customErrors>
    </system.web>
</configuration>

Now add a route entry in your Global.asax that'll handle the url "404" which will pass the request to a controller's action that'll display the View for your 404 page.

Global.asax

routes.MapRoute(
    "404", 
    "404", 
    new { controller = "Commons", action = "HttpStatus404" }
);

CommonsController

public ActionResult HttpStatus404()
{
    return View();
}

All that's left is to add a view for the above action.

One caveat with the above method: according to the book "Pro ASP.NET 4 in C# 2010" (Apress) the use of customErrors is outdated if you're using IIS 7. Instead you should use the httpErrors section. Here's a quote from the book:

But although this setting still works with Visual Studio’s built-in test web server, it’s effectively been replaced by the <httpErrors> section in IIS 7.x.

like image 190
xTRUMANx Avatar answered Oct 03 '22 14:10

xTRUMANx


I'm successfully using this:

return new HttpNotFoundResult();
like image 39
MartinHN Avatar answered Oct 02 '22 14:10

MartinHN


throw new HttpException(404, "NotFound"); along with a custom error handler works fine for me.

like image 38
Darin Dimitrov Avatar answered Sep 29 '22 14:09

Darin Dimitrov


You should use

// returns 404 Not Found as EmptyResult() which is suitable for ajax calls
return new HttpNotFoundResult();

when you are making AJAX calls to your controllers and don't find any content.

When you are making classic calls to controller actions and returning Views you should use:

// throwing new exception returns 404 and redirects to the view defined in web.config <customErrors> section
throw new HttpException(404, ExceptionMessages.Error_404_ContentNotFound);
like image 20
mare Avatar answered Oct 02 '22 14:10

mare


You can personalize 404 result with

return new HttpStatusCodeResult(404, "My message");
like image 42
rdelcoig Avatar answered Oct 03 '22 14:10

rdelcoig