2013-10-18 38 views
0

我正在为“Gone”请求(410代码)创建一个Controller和View,并且我已经完成此操作的方式是将customError状态代码410添加到我的Web.config中。URL如何保持原样但仍呈现错误视图?

这妥善处理异常,并呈现视图,但响应URL是

http://www.mysite.com/Error/Gone?aspxerrorpath=/requested-page

我希望保留原来的URL,同时还显示了飘查看,换句话说,离开网址原因是:“http://www.mysite.com/requested-page

任何想法,我可以开始呢?

+0

只是可以肯定的。你说“customError状态代码10到我的Web.config”这应该是410对吗?如果是,你可以更新这个问题吗? – Spock

+0

我认为你需要这个别名。看看这里http://stackoverflow.com/questions/9853429/how-do-i-create-an-alias-for-a-page-url –

+0

谢谢拉杰,我已经更新了这个问题。并且非常感谢Matt,我会研究别名。 – valin077

回答

0

要保留原始网址,你可以走的路线描述here和明确地处理内部的Global.asax而不是使用web.config中的customErrors错误或httperrors部分(仍然可以配置为回退),但改变 的IHttpHandler.ProcessRequest部分链接的网站示例与HttpContext.Server.TransferRequest(路径,true)。 我实现Application_Error事件像这样在我的项目之一:

protected void Application_Error() 
{ 
    Exception error = Server.GetLastError(); 
    var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500; 

    if (!Context.IsDebuggingEnabled 
     && code != 404) 
    { 
     // persist error to error log db 
     //Log.Logger.Error("unhandled exception: ", exception); 
    } 

    if (WebConfigurationManager.AppSettings["EnableCustomExceptionPage"].Equals("false")) 
    { 
     return; 
    } 

    Response.Clear(); 
    Server.ClearError(); 

    string path = this.Request.Path; 
    string action; 
    switch (code) 
    { 
     case 401: 
      action = "Unauthorized"; 
      break; 
     case 403: 
      action = "Forbidden"; 
      break; 
     case 404: 
      action = "NotFound"; 
      break; 
     default: 
      action = "Generic"; 
      break; 
    } 
    string newPath = string.Format("~/Error/{0}?source={1}&message={2}", action, path, 
            HttpUtility.UrlEncode(error.Message)); 
    Context.Server.TransferRequest(newPath, true); 
} 

内部,它要求一个新的路径传输办理了由ErrorController在描述上面的例子,其行为可能是这样的,即这样的:

public ViewResult Generic(string source, string message) 
{ 
    Response.TrySkipIisCustomErrors = true; 
    Response.StatusCode = 500; 
    ViewBag.Source = source; 
    ViewBag.Message = HttpUtility.UrlDecode(message); 
    return View(); 
} 

public ViewResult NotFound(string source, string message) 
{ 
    Response.TrySkipIisCustomErrors = true; 
    Response.StatusCode = 404; 
    ViewBag.Source = source; 
    ViewBag.Message = HttpUtility.UrlDecode(message); 
    return View(); 
} 

... 

TrySkipIisCustomErrors = true阻止IIS重定向到默认的自定义错误页面。 您可以在Application_Error覆盖方法内,以不同于HttpException的方式处理抛出的异常(如业务/服务层中抛出的特殊异常)。

相关问题