重定向到不同的控制器方法(IIS中的错误。在IIS Express中工作)

时间:2016-06-09 18:50:33

标签: c# asp.net-mvc routing

当在MVC Application_Error例程中处理错误时,下面的代码段应重定向到不同的控制器方法。这在IIS Express中工作正常但在我的测试环境中在完整的IIS中失败。如果传入请求来自Ajax,它应该从单独的客户端ajax请求重定向到Shared控制器方法ErrorDetails。如何调整下面的代码以在IIS Express和IIS中工作?

RequestContext requestContext = ((MvcHandler) httpContext.CurrentHandler).RequestContext;
/* when the request is ajax the system can automatically handle a mistake    with a JSON response. then overwrites the default response */
if (requestContext.HttpContext.Request.IsAjaxRequest())
{
    httpContext.Response.Clear();
    string controllerName = requestContext.RouteData.GetRequiredString("controller");
    IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
    IController controller = factory.CreateController(requestContext, controllerName);
    ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller);


    JsonResult jsonResult = new JsonResult();
    jsonResult.Data = new { result = "Failure", message = Properties.Resources.WebCoreException };
    jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    jsonResult.ExecuteResult(controllerContext);
    httpContext.Response.End();
}

0 个答案:

没有答案