0

I used the method below in the EndRequest method from global asax. I get a HTTP 404 error even from image request or other requests. The point is that I want to check only for the first request/main request. Can you help me please. This is the only solution that works.

{
    if (Context.Response.StatusCode == 404)
    {
        Response.Clear();

        var rd = new RouteData();
        rd.DataTokens["area"] = "AreaName"; 
        rd.Values["controller"] = "Errors";
        rd.Values["action"] = "NotFound";

        IController c = new ErrorsController();
        c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
    }
}
kush
  • 979
  • 1
  • 15
  • 32
Pop Radu
  • 19
  • 5

1 Answers1

0

Assuming you want to only handle 404s for controller actions you can check whether or not the current request is for one like this:

if(!string.IsNullOrEmpty(Request.RequestContext.RouteData.GetRequiredString("controller")))
{
  ...handling code goes here....
}
bluevector
  • 3,437
  • 1
  • 13
  • 18
  • not really i should force him to go in a specific controller and base on the name(some url are known in the db as rots to redirect) so i cannot take this option. There on that unknown links can be a redirect page to some other page from website but i cannot make a difference between them until i get to the specify controller for 404. but i do not get in there with other requests like a image requests. – Pop Radu Oct 18 '12 at 13:14
  • 1
    Then you need to use a regex against the Url or something like that. – bluevector Oct 18 '12 at 13:42