Remove the aspxerrorpath param with CustomErrors use ResponseRewrite

qakmak picture qakmak · Mar 25, 2014 · Viewed 10.3k times · Source

I'm using asp.net MVC4 + visual studio 2012. every thing all fine, But only the custom error always has the aspxerrorpath param on the URL.

I already config the custom error on web.config:

<customErrors mode="On" defaultRedirect="~/Error/Error">
  <error redirect="~/Error/Error" statusCode="404" />
  <error redirect="~/Error/Error" statusCode="500" />
</customErrors>

I also changed my Error Action to :

public ActionResult Error()
{
    Response.Status = "404 Not Found";
    Response.StatusCode = 404;
    return View();
}

Will now when there is some 404 happening. I always got aspxerrorpath param on my URL. I tried add redirectMode="ResponseRewrite" to the customError nodes but If add this , the error will display a run time exception.....

So Is there any best way to remove the aspxerrorpath param? Thanks.

Answer

Mohsen Esmailpour picture Mohsen Esmailpour · Dec 17, 2014

Another simple solution is turning on customErrors in web.config file for 404 error:

<customErrors mode="On">
  <error statusCode="404" redirect="~/home/notfound" />
</customErrors>

and in home controller:

public ActionResult NotFound(string aspxerrorpath)
{
    if (!string.IsNullOrWhiteSpace(aspxerrorpath))
        return RedirectToAction("NotFound");

    return View();
}