本文整理汇总了C#中System.Web.Http.Cors.EnableCorsAttribute类的典型用法代码示例。如果您正苦于以下问题:C# EnableCorsAttribute类的具体用法?C# EnableCorsAttribute怎么用?C# EnableCorsAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EnableCorsAttribute类属于System.Web.Http.Cors命名空间,在下文中一共展示了EnableCorsAttribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//clear default formatters
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
//set formatters only json
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
//jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
示例2: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
//Unity
var container = new UnityContainer();
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<IGenericRepository<Farmacia>, GenericRepository<Farmacia>>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
//Formatters
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
//GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
//config.Formatters.Insert(0, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
// Web API routes
config.MapHttpAttributeRoutes();
//config.EnableCors();
var cors = new EnableCorsAttribute("http://localhost:28285", "*", "*");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
示例3: Register
public static void Register(HttpConfiguration config)
{
var corsConfig = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsConfig);
// Конфигурация и службы Web API
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
// Отключение xml вывода
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
// Настройка Web API для использования только проверки подлинности посредством маркера-носителя.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Маршруты Web API
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
示例4: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Enable cross origin requests to API
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*"
);
config.EnableCors(cors);
//var cors = new EnableCorsAttribute("*", "*", "*");
//config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Remove XML format in order to return JSON
var appXmlType =
config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault
(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
示例5: Register
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}
示例6: Register
public static void Register(HttpConfiguration config)
{
var corsAttr = new EnableCorsAttribute(
ConfigurationManager.AppSettings["EnableCords.Origins"],
ConfigurationManager.AppSettings["EnableCords.Headers"],
ConfigurationManager.AppSettings["EnableCords.Methods"],
ConfigurationManager.AppSettings["EnableCords.ExposedHeaders"]);
config.EnableCors(corsAttr);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApiAction",
routeTemplate: "api/{controller}/{parentId}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MessageHandlers.Add(new TokenValidationHandler(config.DependencyResolver.GetService(typeof(AuthenticationService)) as AuthenticationService));
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.Indent = true;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}
示例7: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var originDomains = System.Configuration.ConfigurationManager.AppSettings["OriginDomains"];
var cors = new EnableCorsAttribute(originDomains, "*", "*") { SupportsCredentials = true };
config.EnableCors(cors);
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var authorisedRoles = System.Configuration.ConfigurationManager.AppSettings["AuthorisedRoles"];
config.Filters.Add(new AuthorizeAttribute
{
Roles = authorisedRoles
});
config.Filters.Add(new ExceptionHandlingAttribute());
// Web API routes
config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
}
示例8: Register
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.Formatters.Remove(config.Formatters.XmlFormatter);
var formatters = config.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Newtonsoft.Json.Formatting.Indented;
//settings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
//var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
//json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
示例9: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
config.Filters.Add(new InvalidModelStateAttribute());
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*");
config.EnableCors(cors);
}
示例10: Register
public static void Register(HttpConfiguration config)
{
//Auth filter - See configuration in web.config
config.Filters.Add(new SeranetAuthAttribute());
// set webapi JSON formatter
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//enable cross domain requests
var corsAttr = new EnableCorsAttribute("*", "*", "*");
corsAttr.SupportsCredentials = true;
config.EnableCors(corsAttr);
// Web API routes
config.MapHttpAttributeRoutes();
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/Seranet.Api.xml")));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
示例11: Register
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Accounts Collection",
routeTemplate: "accountmanagement/accounts",
defaults: new { controller = "Accounts", action = "Index" }
);
config.Routes.MapHttpRoute(
name: "Individual Account",
routeTemplate: "accountmanagement/accounts/{accountId}",
defaults: new { controller = "Accounts", action = "Account" }
);
config.Routes.MapHttpRoute(
name: "Account Followers",
routeTemplate: "accountmanagement/accounts/{accountId}/followers",
defaults: new { controller = "Followers", action = "Index" }
);
}
示例12: Register
public static void Register(HttpConfiguration config)
{
//Enable others ports for any method any header any
var rules = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(rules);
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//Set API to Get JSON not XML.
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
//This is to call the function below.
CreateMaps();
}
示例13: Register
public static void Register(HttpConfiguration config)
{
//Enable CORS
//http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
var cors = new EnableCorsAttribute(ConfigurationManager.AppSettings["CORSServer"] ?? "*", "*", "*");
config.EnableCors();
// Web API configuration and services
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
//attaching the handlers
config.MessageHandlers.Add(new RequestFormatHandler());
// Web API routes
config.MapHttpAttributeRoutes();
/*
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
*/
}
示例14: Register
public static void Register(HttpConfiguration config)
{
// Force load of all Controllers:
if (Current.WebApiCORSDomains != null)
{
var items = Current.WebApiCORSDomains.Split(',');
var corsAttr = new EnableCorsAttribute(Current.WebApiCORSDomains, "*", "*") {SupportsCredentials = true};
Current.Log.Add("WebApi REST : {0} CORS domains allowed.".format(items.Length), Message.EContentType.Info);
config.EnableCors(corsAttr);
}
config.Services.Add(typeof(IExceptionLogger), new GlobalErrorHandler());
config.SuppressHostPrincipal(); //Isolates WebApi Auth form Host (IIS) Auth
config.Filters.Add(new NyanAuthenticationFilter());
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new CsvMediaTypeFormatter());
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
//config.Formatters.Add(new CsvMediaTypeFormatter());
Current.Log.Add("WebApi REST : Routes registered.", Message.EContentType.Info);
}
示例15: Register
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiWithActionName",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}