42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Web.Http;
|
|
using System.Web.Http.Cors;
|
|
|
|
namespace GSYWApi
|
|
{
|
|
/// <summary>
|
|
/// WebApi配置信息
|
|
/// </summary>
|
|
public static class WebApiConfig
|
|
{
|
|
/// <summary>
|
|
/// 注册事件
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
public static void Register(HttpConfiguration config)
|
|
{
|
|
//跨域配置
|
|
var allowedMethods = ConfigurationManager.AppSettings["cors:allowedMethods"];
|
|
var allowedOrigin = ConfigurationManager.AppSettings["cors:allowedOrigin"];
|
|
var allowedHeaders = ConfigurationManager.AppSettings["cors:allowedHeaders"];
|
|
var geduCors = new EnableCorsAttribute(allowedOrigin, allowedHeaders, allowedMethods)
|
|
{
|
|
SupportsCredentials = true
|
|
};
|
|
config.EnableCors(geduCors);
|
|
//config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
|
|
// Web API 路由
|
|
config.MapHttpAttributeRoutes();
|
|
|
|
config.Routes.MapHttpRoute(
|
|
name: "DefaultApi",
|
|
routeTemplate: "api/{controller}/{id}",
|
|
defaults: new { id = RouteParameter.Optional }
|
|
);
|
|
config.Formatters.Remove(config.Formatters.XmlFormatter);
|
|
}
|
|
}
|
|
}
|