简体   繁体   English

自定义“找不到与请求uri匹配的http资源”消息

[英]Customizing “No http resource was found that matches the request uri” message

Is there a simple way to modify the default 404 message returned by Web API? 是否有一种简单的方法来修改Web API返回的默认404消息?

No http resource was found that matches the request uri 找不到与请求uri匹配的http资源

The simple way to do that is by "DelegatingHandler" 这样做的简单方法是“DelegatingHandler”

  1. First Step is to create a new class inherits from DelegatingHandler: 第一步是创建一个继承自DelegatingHandler的新类:

      public class ApiGatewayHandler : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = await base.SendAsync(request, cancellationToken); if (response!=null && response.StatusCode == HttpStatusCode.NotFound) { var msg = await response.Content.ReadAsStringAsync(); //you can change the response here if (msg != null && msg.Contains("No HTTP resource was found")) { return new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, Content = new ObjectContent(typeof(object), new { Message = "New Message..No HTTP resource was found that matches the request URI" }, new JsonMediaTypeFormatter()) }; } } return response; return response; } 

    } }

  2. Then Register this class in the web api Register config file 然后在web api注册配置文件中注册此类

     public static void Register(HttpConfiguration config){ public static void Register(HttpConfiguration config) { // you config and routes here config.MessageHandlers.Add(new ApiGatewayHandler()); //.... } } 

That is it. 这就对了。 Same way if you need to change any other error message. 如果您需要更改任何其他错误消息,也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到与Angular 2中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in Angular 2 找不到与请求URI&#39;MyUrl&#39;匹配的HTTP资源 - No HTTP resource was found that matches the request URI 'MyUrl' 获取没有与请求URI匹配的HTTP资源 - Getting No HTTP resource was found that matches the request URI 找不到与请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI 找不到与webapi中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in webapi 未找到与请求URI匹配的HTTP资源,未找到与控制器匹配的类型 - No HTTP resource was found that matches the request URI, No type was found that matches the controller 路由错误:找不到与请求URI匹配的HTTP资源 - Routing error: No HTTP resource was found that matches the request URI 未找到与请求 URI 匹配的 HTTP 资源,我需要解决方案 - No HTTP resource was found that matches the request URI, I need solutions 我收到错误“没有找到与请求 URI 匹配的 HTTP 资源” - I am getting error “No HTTP resource was found that matches the request URI” 为什么这里“找不到与请求 URI 匹配的 HTTP 资源”? - Why is it that "No HTTP resource was found that matches the request URI" here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM