Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the aspxerrorpath param with CustomErrors use ResponseRewrite

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.

like image 839
qakmak Avatar asked Mar 25 '14 09:03

qakmak


People also ask

How do I turn off custom error mode?

Click the Custom Errors tab. Select Off for custom error mode. Or navigate to the folder containing your application and open the web. config file in a text editor and edit by hand, and change the custom errors tag to <customErrors mode="Off" />.

How to handle error in web config?

You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file. The customErrors section allows you to specify a default page that users will be redirected to when an error occurs. It also allows you to specify individual pages for specific status code errors.

What is the use of defaultRedirect attribute in error handling?

Use the defaultRedirect to specify where an http request should be redirect by default if an error occurs. You can specify the name of a webform, an HTML, or an action method. This will redirect to this page on any error code, not just 500.


1 Answers

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();
}
like image 102
Mohsen Esmailpour Avatar answered Oct 18 '22 17:10

Mohsen Esmailpour