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
  • 17
  • 5
  • What do you mean by "main"? Are you only interested in handling 404s for controller actions? – bluevector Oct 18 '12 at 12:50
  • by main let say you make a request to http://www.microsoft.comand if you lock in firebug you will see server other requests for images and stuff – Pop Radu Oct 18 '12 at 12:52
  • put your error here. (stacktrace) – Inbar Rose Oct 18 '12 at 12:54
  • is not an error with stack tree the point in here is that you got a 404 request. This request can come from mysite.com/blabla <- here you will get a 404 page not found. but 404 you get even if in your site have a request of a image like /myImage.jpg or someother url is not found. What i want to get is the 404 only for mysite.com/blabla. here you can read about this 404 request more http://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling on the answer of Marco (35 ups) – Pop Radu Oct 18 '12 at 12:58

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,485
  • 1
  • 15
  • 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