Remove the aspxerrorpath param with CustomErrors use ResponseRewrite
Asked Answered
F

7

9

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.

Faintheart answered 25/3, 2014 at 9:53 Comment(3)
@AmirHossein Mehrvarzi Looks like you're not see my question, I said I already tried solution: redirectMode="ResponseRewrite", but It's not working.Faintheart
@AmirHossein Mehrvarzi these problems already finish, but no any one can fixed my problem.Faintheart
@AmirHosseinMehrvarzi, I fixed it use another way, you need to remove the Already answer link.Faintheart
O
7

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();
}
Omen answered 17/12, 2014 at 20:24 Comment(0)
M
2

I wrote

<customErrors mode="On" defaultRedirect="~/" redirectMode="ResponseRedirect" />

in <system.web>...</system.web>, than in my default action's (in the default controller), on the first line I wrote following:

if (Request["aspxerrorpath"] != null) return RedirectToAction(this.ControllerContext.RouteData.Values["action"].ToString());

It is dirty resolve, but effective.

Micah answered 10/2, 2016 at 6:19 Comment(0)
F
1

Finally I close the customErrors

<customErrors  mode="Off"></customErrors>

Then I use httpErrors to replace it.

<httpErrors  errorMode="Custom" existingResponse="Replace">
      <remove statusCode="400"/>
      <error statusCode="400" responseMode="ExecuteURL" path="/Error/BadRequest"/>
      <remove statusCode="403"/>
      <error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
          <remove statusCode="404"/>
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
      <remove statusCode="500"/>
      <error statusCode="500" responseMode="ExecuteURL" path="/Error/Error" />
</httpErrors>

Now All fine.

Update 1

But it will display the exception details sometimes , So now I use :

<customErrors mode="RemoteOnly" defaultRedirect="~/Main/Error" redirectMode="ResponseRewrite">
      <error redirect="~/Main/NotFound" statusCode="404" />
      <error redirect="~/Main/Error" statusCode="500" />
      <error redirect="~/Main/AccessDenied" statusCode="403" />
      <error redirect="~/Main/BadRequest" statusCode="400" />
    </customErrors> 


 <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="400" />
      <error statusCode="400" responseMode="ExecuteURL" path="/Main/BadRequest" />
      <remove statusCode="403" />
      <error statusCode="403" responseMode="ExecuteURL" path="/Main/AccessDenied" />
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Main/NotFound" />
      <remove statusCode="500" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Main/Error" />
    </httpErrors>

I think still not find best solution for now. also don't understand why the Microsoft design like that. Maybe want every one know the website based on .NET

Faintheart answered 16/12, 2014 at 10:41 Comment(0)
M
0

I have same issue the aspx error path is set from my IIS server. so check your IIS settings:

your project > Authentication > Forms Authentic

In form authentication right click Edit and check your URL.

I have problem in that url path remove the extension .aspx and it working fine.

Manufacturer answered 25/3, 2014 at 10:6 Comment(3)
Hi, I'm not use any Authentication in my project. and I'm using IIS7.5 with it . I'm use MVC4. not use .aspx view pageFaintheart
@Faintheart i am using MVC4 razor syntax with iis7. but it automatically add .aspx at the end of my default page.Manufacturer
Not mine, I'm alredy remove aspx view engine from Global.asax use :ViewEngines.Engines.Clear();. my error url like:127.0.0.1/Error/Error?aspxerrorpath=Faintheart
A
0

Use Redirect inside your mvc action.

For example in config you can have "~/Error", in controller have Index action (default action) that redirects to Error action (I would call it PageNotFound) - querystring will be lost on redirecting.

Afrika answered 14/12, 2014 at 22:44 Comment(0)
F
0
<customErrors mode="On" defaultRedirect="/404.html"  redirectMode="ResponseRewrite">
    <error redirect="/403.html" statusCode="403"  />
    <error redirect="/404.html" statusCode="404" />
    <error redirect="/500.html" statusCode="500" />
  </customErrors>
Forestation answered 15/10, 2018 at 9:0 Comment(0)
A
0

The absolute simplest solution is to use a blank querystring in the defaultRedirect in web.config to override (note the question mark):

<system.web>
    <customErrors defaultRedirect="~/error?" mode="On" />
</system.web>
Ataliah answered 28/5, 2020 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.