【发布时间】: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