【问题标题】:Modify the response so it routes the client to my Error.cshtml page?修改响应以将客户端路由到我的 Error.cshtml 页面?
【发布时间】:2016-06-03 17:33:41
【问题描述】:

我的服务器端代码中有一个 http 处理程序。 所有响应都通过处理程序。我正在实现检查对客户端的响应是否为 2xx 响应的代码,否则(即 404)会路由到我的自定义错误页面。

最终,根据我检查请求和响应的某些标准,我将有一个不同的错误页面,但首先我只需要知道如何在我的视图中重新路由到一个简单的error.cshtml 页面。

如何修改响应,以便将客户端路由到我的 Error.cshtml 页面(在视图中),而不是使用 404 进行响应?

namespace Mymodels.Models
{
    public class myHandler : DelegatingHandler
    {
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {            
            var response = await base.SendAsync(request, cancellationToken);

            if (!response.IsSuccessStatusCode) //inspects outgoing response to check if status code is 2xx
            {
                //do response work here since response is 2xx
            }
            else
            {
               //How do I modify the reponse to Error.cshtml page?, which is in my Views folder

            }
            return response;
        }
    }
}

【问题讨论】:

  • 我认为您将其添加到 else throw new HttpException(404, "Not found"); 并且配置将是` `
  • @AbdelrhmanMohamed 我不认为这对我有用,因为我将根据请求的内容将它们重新路由到各种错误页面。意思是,我通过读取请求并将它们重新路由到错误页面来处理 404 错误,该页面将包含有关如何获取所需信息的信息。因此它将是各种错误页面,它们是根据他们的要求定制的。
  • 我认为在这种情况下你不需要使用 HTTP 错误代码,除非与 404, 403,401 相关的东西你只需要返回带有所需 html 错误的响应

标签: c# asp.net-mvc-4 model-view-controller


【解决方案1】:

您可以在 global.asax 中使用 Application_Error 处理程序。

protected void Application_Error(object sender, EventArgs e) {

    Exception exception = Server.GetLastError();
    Response.Clear();

    HttpException httpException = exception as HttpException;

    if (httpException != null) {
        string action;

        switch (httpException.GetHttpCode()) {
            case 404:
                action = "HttpError404";
                break;
            case 500:
                action = "HttpError500";
                break;
            default:
                action = "HttpErrorGeneric";
                break;
        }

        RouteData routeData = new routeData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", action);
        routeData.Values.Add("Summary","Error");
        routeData.Values.Add("Description", ex.Message);
        IController controller = New ErrorController()
        controller.Execute(New RequestContext(New HttpContextWrapper(Context), routeData));
    }
}

【讨论】:

    【解决方案2】:

    我的解决方案是使用HttpContext.Current.Response.RedirectToRoute:

    namespace Mymodels.Models
    {
        public class myHandler : DelegatingHandler
        {
            protected async override Task<HttpResponseMessage> SendAsync(
                HttpRequestMessage request, CancellationToken cancellationToken)
            {            
                var response = await base.SendAsync(request, cancellationToken);
    
                 if (response.StatusCode != HttpStatusCode.NotFound) //if response is NOT 404
                {
                    //do response work here since response is not 404
                }
                else
                {
                    response.StatusCode = HttpStatusCode.Redirect; //set status code to 302
                    HttpContext.Current.Response.RedirectToRoute("customErrorRequestPage", "Home"); 
    
                }
                return response;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2018-02-05
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多