一、添加webapi项目
二、nuget下载webapi所需的类库引用
install-package microsoft.aspnet.webapi
install-package microsoft.owin.host.systemweb
install-package microsoft.aspnet.webapi.cors
三、webapi基础配置
配置webapi路由、跨域支持
public static class webapiconfig { public static void register(httpconfiguration config) { //跨域配置 config.enablecors(new enablecorsattribute("*", "*", "*")); //webapi路由 config.maphttpattributeroutes(); //设置webapi路由规则 config.routes.maphttproute( name: "areaapi", routetemplate: "api/{area}/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional } ); config.routes.maphttproute( name: "webapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional } ); //移除xml返回格式数据 globalconfiguration.configuration.formatters.xmlformatter.supportedmediatypes.clear(); //配置返回的时间类型数据格式 globalconfiguration.configuration.formatters.jsonformatter.serializersettings.converters.add( new newtonsoft.json.converters.isodatetimeconverter() { datetimeformat = "yyyy-mm-dd hh:mm:ss" } ); } }
注册webapi
public class webapiapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); globalconfiguration.configure(webapiconfig.register); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); } }
黄山市民网:https://www.huangshanshimin.com/