Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';在c#web api中重定向HTTP响应的正确方法是什么?_C#_Asp.net_Redirect_Routes_Response - Fatal编程技术网

什么';在c#web api中重定向HTTP响应的正确方法是什么?

什么';在c#web api中重定向HTTP响应的正确方法是什么?,c#,asp.net,redirect,routes,response,C#,Asp.net,Redirect,Routes,Response,下面是我们网站的回复示例 HTTP响应代码为404(未找到) { “消息”:“未找到与请求URI“”匹配的HTTP资源。”, “MessageDetail”:“在控制器‘controller’上未找到与请求匹配的操作。”} 我们应该能够将HTTP响应修改为403(错误请求),因为用户返回的参数不完整(还有另一个参数,例如parameter2),这也是必需的,并且我们还应该将消息细节修改为可定制的内容 这是可以实现的吗 下面是WebApiConfig.cs中的示例方法 var cors = new

下面是我们网站的回复示例

HTTP响应代码为404(未找到)

{ “消息”:“未找到与请求URI“”匹配的HTTP资源。”, “MessageDetail”:“在控制器‘controller’上未找到与请求匹配的操作。”}

我们应该能够将HTTP响应修改为403(错误请求),因为用户返回的参数不完整(还有另一个参数,例如parameter2),这也是必需的,并且我们还应该将消息细节修改为可定制的内容

这是可以实现的吗

下面是WebApiConfig.cs中的示例方法

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

我已尝试删除config.maphttpAttribute(),但如果参数不匹配,应用程序不会转到控制器,因此我可以返回响应。

您可以创建中间件类,在该类中可以检查和修改HTTP请求数据(参数、标头、路由、返回状态代码结束等)在控制器获取之前和控制器执行之后。 下面是中间件的示例