当前位置:   article > 正文

webApi接口返回json对象_webapi delete 返回json

webapi delete 返回json

App_Start文件中配置WebApiConfig:配置如下

代码:

  1. public static class WebApiConfig
  2. {
  3. public static void Register(HttpConfiguration config)
  4. {
  5. // Web API 配置和服务
  6. // Web API 路由
  7. config.MapHttpAttributeRoutes();
  8. config.Routes.MapHttpRoute(
  9. name: "DefaultApi",
  10. routeTemplate: "api/{controller}/{action}/{id}",
  11. defaults: new { id = RouteParameter.Optional }
  12. );
  13. //var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
  14. //jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  15. var jsonFormatter = new JsonMediaTypeFormatter();
  16. config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
  17. }
  18. }
  19. public class JsonContentNegotiator : IContentNegotiator
  20. {
  21. private readonly JsonMediaTypeFormatter _jsonFormatter;
  22. public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
  23. {
  24. _jsonFormatter = formatter;
  25. }
  26. public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
  27. {
  28. var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
  29. return result;
  30. }
  31. }

控制器一点要继承ApiController

字符串转object

var result=JSON.parse(coderes.result);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/550398
推荐阅读
相关标签
  

闽ICP备14008679号