views:

244

answers:

1

Working in ASP.net 3.5 and MVC 1.0.

What I would like to do is return the requested URL, which generates a 404 error, within the custom error page. Much like Google does on their error pages (http://www.google.com/test).

eg.

We're sorry, but the requested address "http://www.domain.com/nonexistantpage.aspx" does not exist on this server.

What would be the best way to accomplish this kind of soft 404?

Also, as a side note: Anyone familiar with returning the custom error page in place of the ugly ...notfound?aspxerrorpath=/awdawd nonsense, while keeping the requested URL in a browser's address bar? ...I suspect something to do with a server.transfer?

A: 

Check out these resources related to this topic:

To summarize, you can accomplish a google-like implementation with keeping the requested URL by defining a catch-all route that executes a particular controller action.

//defined below all other routes
routes.MapRoute(
    "Catch All",
    "{*path}", 
    new { controller = "Error", action = "NotFound" }
);

public ActionResult NotFound(string path)
{
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    ViewData["path"] = path; //or Request.Url.ToString() if you want full url
    return View();
}

This is not a complete solution, though. Assuming you've left the default route mapping, anything that matches {Controller}/{action}/{id} is still going to a throw a traditional 404 or custom error. You'd have to explicitly define all possible routes if you truly wanted to have the catch-all route pick up anything that didn't map to a specific controller/action or parameter type - not necessarily a trivial task.

Kurt Schindler