赞
踩
API完整代码
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Web.Http.Description;
-
- namespace WebSocket.Api
- {
- /// <summary>
- /// List文章列表
- /// </summary>
- public class ListController : ApiController
- {
- // GET api/<controller>
- /// <summary>
- /// Listjson方法
- /// </summary>
- /// <returns></returns>
- [AcceptVerbs("GET", "POST")]
- [Route("API/List/Listjson/")]
- [ResponseType(typeof(ReturnResult))]
- public HttpResponseMessage Listjson()
- {
- ReturnResult result = new ReturnResult() {code = 0,msg="成功"};
- result.data = JsonConvert.SerializeObject(new string[] { "value1", "value2" });
- string json = JsonConvert.SerializeObject(result);
- return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
- }
-
- }
-
- internal class ReturnResult
- {
- /// <summary>
- ///数据状态一切正常的状态码 0:成功,1:失败
- /// </summary>
- public int code { get; set; } = 1;
-
- /// <summary>
- /// 状态信息
- /// </summary>
- public String msg { get; set; } = "失败";
-
- /// <summary>
- /// 数据详情
- /// </summary>
- public string data { get; set; }
-
- /// <summary>
- /// 数据总条数
- /// </summary>
- public int count { get; set; } = 0;
- }
- }
[AcceptVerbs("GET", "POST")]
[Route("API/List/Listjson/")]
特性路由开启
项目》App_Start》RouteConfig.cs
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
完整代码
- namespace WebSocket
- {
- public class RouteConfig
- {
-
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapMvcAttributeRoutes();
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
- }
[ResponseType(typeof(ReturnResult))]
这个特性需要引入
using System.Web.Http.Description;
里面的typeof(ReturnResult)的ReturnResult是一个模型类,上面的“API完整代码”有写
https://blog.csdn.net/cplvfx/article/details/108716279
可能你还需要
https://blog.csdn.net/cplvfx/article/details/108510697
参考:http://blchen.com/change-asp-net-web-api-default-return-type-to-json/
ASP.NET Web API的controller默认的返回数据格式是XML格式,要改成返回JSON格式只需要在WebApiConfig.cs文件里的Register方法末尾加上如下代码
- var json = config.Formatters.JsonFormatter;
- json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
- json.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//返回时间格式化
- config.Formatters.Remove(config.Formatters.XmlFormatter);
WebApiConfig.cs 完整代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Http;
-
- namespace HaoSiJiaWeb
- {
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- // Web API 配置和服务
-
- // Web API 路由
- config.MapHttpAttributeRoutes();
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
-
- var json = config.Formatters.JsonFormatter;
- json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
- json.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//返回时间格式化
- config.Formatters.Remove(config.Formatters.XmlFormatter);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。