0

I have Existing MVC Web Application url physically not exists (i.e. MyMVCSite/mypage.aspx. i need to redirect page on "error page" if invalid aspx enter by user, which not working in .aspx page condition but when unvalid action enter its work

-) MVCSite/InvalidePage --> Redirect to error Page MVCSite/error

-) MVCSite/InvalidePage.aspx --> Redirect to HomePage as Page MVCSite/InvalidePage.aspx

I need last condition also redirects to Page MVCSite/error So this condition is also tried by me as URL physically not exist its also not work here...

protected void Application_BeginRequest(object sender, EventArgs e)
{ 
    if (sUrl.EndsWith(".aspx"))
    { 
        string[] path = sUrl.Split('/');
        if (!System.IO.File.Exists(Server.MapPath("test.aspx")))
        Response.Redirect("error");
    } 
}

Furthermore i am unable to apply 404 exception in Application_Error event in Global.asax, this exception occurs many time so there is also a checkd of 404 - File does not exist due to some unknow reasons may be some images, css file not find which currently very hard to find

protected void Application_Error()
{
    if (objException.Message != "File does not exist.") { //..... } 
}

I am also apply custom errors in Web.config which also not work

<customErrors mode="Off">
    <error statusCode="404" redirect="Error/Index" />
</customErrors>

Currently error page only occur when action name is wrong but if page name is wrong it redirect us on home page with wrong url Please advice many any other option using that i would resolve this issue

Xaruth
  • 4,034
  • 3
  • 19
  • 26
taha ahmed
  • 19
  • 6

2 Answers2

2

Have a look at this link, this will surely help

look at http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/

url says mvc-2 but it is similar for all versions

also this

http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx

For handling with Global.asax file

Instead of creating a new route for that, you could just redirect to your controller/action and pass the information via querystring. For instance:

protected void Application_Error(object sender, EventArgs e) {
  Exception exception = Server.GetLastError();
  Response.Clear();

  HttpException httpException = exception as HttpException;

  if (httpException != null) {
    string action;

    switch (httpException.GetHttpCode()) {
      case 404:
        // page not found
        action = "HttpError404";
        break;
      case 500:
        // server error
        action = "HttpError500";
        break;
      default:
        action = "General";
        break;
      }

      // clear error on server
      Server.ClearError();

      Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
    }

Then your controller will receive whatever you want:

// GET: /Error/HttpError404
public ActionResult HttpError404(string message) {
   return View("SomeView", message);
}

There are some tradeoffs with your approach. Be very very careful with looping in this kind of error handling. Other thing is that since you are going through the asp.net pipeline to handle a 404, you will create a session object for all those hits. This can be an issue (performance) for heavily used systems.

GlobalASAX solution Source

Community
  • 1
  • 1
0

You currently have 'customErrors' switched off, this should be switched on.

<customErrors mode="Off">
  <error statusCode="404" redirect="Error/Index" />
</customErrors>

Also another way of redirecting would be checking to see if the response is not equal to null within the controller code, example below:

if (userId == null)
{
  return RedirectToAction("Error404", "Error");
}
else
{
  //Process the request
}
Damian
  • 574
  • 1
  • 6
  • 19
  • problem is that it cannot redirect on error Page, how it should work goto controller class as used by you in which you RedirectToAction – taha ahmed Oct 14 '13 at 21:57