赞
踩
App_Start文件中配置WebApiConfig:配置如下
代码:
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- // Web API 配置和服务
-
- // Web API 路由
- config.MapHttpAttributeRoutes();
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{action}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
-
- //var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
- //jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
- var jsonFormatter = new JsonMediaTypeFormatter();
- config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
- }
- }
- public class JsonContentNegotiator : IContentNegotiator
- {
- private readonly JsonMediaTypeFormatter _jsonFormatter;
-
- public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
- {
- _jsonFormatter = formatter;
- }
-
- public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
- {
- var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
- return result;
- }
- }
控制器一点要继承ApiController
字符串转object
var result=JSON.parse(coderes.result);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。