.net跨域和输出格式化
程序员文章站
2022-09-26 21:53:35
using System.Web.Http; using System.Web.Http.Cors; using System.Net.Http.Headers; namespace WebAPI { public static class WebApiConfig { public static ......
using system.web.http; using system.web.http.cors; using system.net.http.headers; namespace webapi { public static class webapiconfig { public static void register(httpconfiguration config) { //跨域配置 config.enablecors(new enablecorsattribute("*", "*", "*")); // web api 配置和服务 config.formatters.jsonformatter.serializersettings.datetimezonehandling = newtonsoft.json.datetimezonehandling.local;// convert all datetime to utc config.formatters.jsonformatter.supportedmediatypes.add(new mediatypeheadervalue("text/html")); config.formatters.jsonformatter.supportedmediatypes.add(new mediatypeheadervalue("text/xml")); config.formatters.jsonformatter.supportedmediatypes.add(new mediatypeheadervalue("text/json")); // web api 路由 config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } } }
推荐阅读