Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC redirect to error page from controller

Tags:

asp.net-mvc

I have a simple question. How to redirect from controller to custom error page with http status code? For example:

public ActionResult Index()
{
   this.Redirect("Not found", 404); //redirect to my error page with code 404
}
like image 698
bluray Avatar asked Jan 30 '17 11:01

bluray


1 Answers

add this code in web.config file:-

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

Add below code in controller:

public class ErrorController : Controller{
public ViewResult Index()
{
    return View("Error");
}
public ViewResult NotFound()
{
    Response.StatusCode = 404;  //you may want to set this to 200
    return View("NotFound");
}}
like image 141
Sarika Koli Avatar answered Sep 18 '22 12:09

Sarika Koli