要领一:(改设置法)
找到Global.asax文件,在Application_Start()要领中增加一句:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
修改后:
<br>protected void Application_Start() <br>{ <br>AreaRegistration.RegisterAllAreas(); <br>WebApiConfig.Register(GlobalConfiguration.Configuration); <br>FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); <br>RouteConfig.RegisterRoutes(RouteTable.Routes); <br>// 使api返回为jsonGlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();} <br>
如许返回的效果就都是json范例了,但有个不好的处所,假如返回的效果是String范例,如123,返回的json就会变成"123";
处置惩罚的要领是自定义返回范例(返回范例为HttpResponseMessage)
public HttpResponseMessage PostUserName(User user) { String userName = user.userName; HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(userName,Encoding.GetEncoding("UTF-8"), "application/json") }; return result; }
要领二:(万金油法)
要领一中又要改设置,又要处置惩罚返回值为String范例的json,甚是贫苦,不如就不必web api中的的自动序列化对象,本身序列化后再返回
public HttpResponseMessage PostUser(User user) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string str = serializer.Serialize(user); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; }
要领二是我比较引荐的要领,为了不在每一个接口中都反复写那几句代码,所以就封装为一个要领如许运用就轻易多了。
public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String ||obj is Char) { str = obj.ToString(); } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); str = serializer.Serialize(obj); } HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; }
要领三:(最贫苦的要领)
要领一最简朴,但杀伤力太大,一切的返回的xml花样都会被毙掉,那末要领三就能够只让api接口中毙掉xml,返回json
先写一个处置惩罚返回的类:
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; } }
找到App_Start中的WebApiConfig.cs文件,翻开找到Register(HttpConfiguration config)要领
增加以下代码:
var jsonFormatter = new JsonMediaTypeFormatter(); config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
增加后代码以下:
<br>public static void Register(HttpConfiguration config) <br>{ <br>config.Routes.MapHttpRoute( <br>name: "DefaultApi", <br>routeTemplate: "api/{controller}/{action}/{id}", <br>defaults: new { id = RouteParameter.Optional } <br>);var jsonFormatter = new JsonMediaTypeFormatter(); onfig.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));} <br>
要领三假如返回的效果是String范例,如123,返回的json就会变成"123",处置惩罚要领同要领一。
实在web api会自动把返回的对象转为xml和json两种花样并存的情势,要领一与要领三是毙掉了xml的返回,而要领二是自定义返回。
以上就是C# web api返回范例设置为json的两种要领的内容,更多相关内容请关注ki4网(www.ki4.cn)!