开发者

Returning Requested URL within Custom Error Page in ASP.net

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 t开发者_运维知识库his 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?


Check out these resources related to this topic:

  • ASP.Net MVC Custom Error Pages
  • Three common ASP.NET MVC URL routing issues

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜