public static void Register(HttpConfiguration config) { var cors = new EnableCorsAttribute("*", "*", "GET, POST, PUT, DELETE, OPTIONS", "Signature"); config.EnableCors(); // Web API 配置和服务 config.Formatters.Clear(); var jsonFormatter = new JsonMediaTypeFormatter(); // Convert all dates to UTC type jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; // Convert all dates to Microsoft type("\/Date(ticks)\/ ") //jsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All; jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Formatters.Add(jsonFormatter); //config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
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 } ); }
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(); }
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 } ); }
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" } ); }
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(); }
public static void Register(HttpConfiguration config) { //config.EnableCors(new EnableCorsAttribute("*", "*", "*")); config.EnableCors(); var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"); //config.EnableCors(cors); // Web API configuration and services // Configure Web API to use only bearer token authentication. config.EnableCors(cors); config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); //GlobalConfiguration.Configuration.MessageHandlers.Add(new Models.CustomLogDelegatHandler()); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); }
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 } ); */ }
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); }
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(); }
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 } ); }
private static HttpSelfHostConfiguration GetSettings() { var localhostAddress = ConfigurationManager.AppSettings[SettingsHelper.KEY_LOCALHOST_ADDRESS]; var serverConfiguration = new HttpSelfHostConfiguration(localhostAddress); serverConfiguration.Filters .Add(new ExceptionResponseFilterAttribute()); serverConfiguration.Routes .MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{parameters}", defaults: new { parameters = RouteParameter.Optional } ); var xmlFormatter = serverConfiguration.Formatters.OfType<XmlMediaTypeFormatter>().First(); var jsonFormatter = serverConfiguration.Formatters.OfType<JsonMediaTypeFormatter>().First(); var cors = new EnableCorsAttribute("*", "GET", "GET"); serverConfiguration.EnableCors(cors); serverConfiguration.Formatters.Remove(xmlFormatter); jsonFormatter.SerializerSettings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }; return serverConfiguration; }
// And now, we add a new constructor that accepts a profile name /// <summary> /// Initializes a new instance of the <see cref="EnableCorsAttribute" /> class. /// </summary> /// <param name="corsProfile">Name of the profile.</param> public EnableCorsAttribute(string corsProfile) { if (string.IsNullOrEmpty(corsProfile)) { throw new ArgumentException(StringResources.ArgumentCannotBeNullOrEmpty, "corsProfile"); } //Look up the CorsProfile var corsSettings = GetProfile(corsProfile); if (corsSettings == null) { throw new ConfigurationErrorsException(string.Format(StringResources.ErrNoCorsProfileFoundMatchingName, corsProfile)); } _internalAttribute = new System.Web.Http.Cors.EnableCorsAttribute( corsSettings.Origins, corsSettings.Headers, corsSettings.Methods, corsSettings.ExposedHeaders ); if (corsSettings.PreflightMaxAge > -1) { _internalAttribute.PreflightMaxAge = corsSettings.PreflightMaxAge; } _internalAttribute.SupportsCredentials = corsSettings.SupportsCredentials; }
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); }
public static void Register(HttpConfiguration config) { var cors = new EnableCorsAttribute("http://src.streamus.com,http://dist.streamus.com,https://streamus.com,chrome-extension://jbnkffmindojffecdhbbmekbmkkfpmjd,chrome-extension://nnmcpagedcgekmljdamaeahfbmmjloho", "*", "*"); config.EnableCors(cors); AutofacRegistrations.RegisterAndSetResolver(config); }
public static void Register(HttpConfiguration config) { // Web API 設定和服務 //------------------------------------ // Exceptionless ExceptionlessClient.Default.Configuration.UseTraceLogger(); ExceptionlessClient.Default.Configuration.UseReferenceIds(); ExceptionlessClient.Default.RegisterWebApi(config); // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // 移除XML格式輸出 config.Formatters.Remove(config.Formatters.XmlFormatter); // 置換錯誤攔截器 config.Services.Replace(typeof(IExceptionHandler), new ErrorHandler()); // 允許CORS var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); }
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 } ); }
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)); // Enable cors EnableCorsAttribute corsAttr = new EnableCorsAttribute("http://localhost:51227", "*", "*"); config.EnableCors(corsAttr); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApiWithActions", routeTemplate: "api/{controller}/{action}/{param1}/{param2}", defaults: new { param1 = RouteParameter.Optional, param2 = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{param1}/{param2}", defaults: new { param1 = RouteParameter.Optional, param2 = RouteParameter.Optional } ); }
public static void Register(HttpConfiguration config) { var cors = new EnableCorsAttribute("http://localhost:8080", "*", "*"); config.EnableCors(cors); // Web API configuration and services var container = new UnityContainer(); container.RegisterType<IRepository<User>, BaseRepository<User>>() .RegisterType<IUserService, UserService>() .RegisterType<IRepository<UserBlog>, BaseRepository<UserBlog>>() .RegisterType<IUserBlogService, UserBlogService>() .RegisterType<IRepository<Post>, BaseRepository<Post>>() .RegisterType<IPostService, PostService>() .RegisterType<IRepository<PipelineView>, BaseRepository<PipelineView>>() .RegisterType<IPipelineService, PipelineService>(); config.DependencyResolver = new UnityResolver(container); AutoMapperConfiguration.Configure(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API configuration and services var cors = new EnableCorsAttribute( "*", "*", "*"); config.EnableCors(cors); config.Formatters.Add(new BinaryMediaTypeFormatter()); var httpControllerRouteHandler = typeof(HttpControllerRouteHandler).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic); httpControllerRouteHandler?.SetValue(null, new Lazy<HttpControllerRouteHandler>(() => new Members.Controllers.SessionEnabledHttpControllerRouteHandler(), true)); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Services.Add(typeof(IExceptionLogger), new Members.App_Start.AiExceptionLogger()); // Capture exceptions for Application Insights: }
private static HttpConfiguration BuildHttpConfiguration() { var configuration = new HttpConfiguration(); // configure JSON serialization var serializerSettings = configuration.Formatters.JsonFormatter.SerializerSettings; serializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; serializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified; #if DEBUG serializerSettings.Formatting = Formatting.Indented; #endif serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); serializerSettings.Converters.Add(new StringEnumConverter()); // enable cors since web server is different port than api server var cors = new EnableCorsAttribute("*", "*", "*"); configuration.EnableCors(cors); // use attribute routing configuration.MapHttpAttributeRoutes(); // custom factory for web api controllers configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorHttpControllerActivator()); // Global Exception Logger (log exceptions to NLog) configuration.Services.Add(typeof(IExceptionLogger), new NLogExceptionLogger()); return configuration; }
public static void Register(HttpConfiguration config) { // Stop IIS/Asp.Net breaking our routes // taken from here: // http://www.grumpydev.com/2013/09/17/403-14-error-when-trying-to-access-a-webapi-route/ // TLDR; if IIS sees a url match a project folder name, all routes will be ignored // this setting will prevent that RouteTable.Routes.RouteExistingFiles = true; // enable Cross-Origin Resource Sharing (CORS) var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); // Define routes config.Routes.MapHttpRoute( name: DEFAULT_ROUTE_NAME, // {controller} will match a convention name // (i.e. http get to http://url/demo will invoke DemoController.get... method without an id) routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.EnableSystemDiagnosticsTracing(); // Register message handlers config.MessageHandlers.Add(new SSLHandler()); // Register global authentication/authorization filters config.Filters.Add(new AuthFilter(AuthFilter.AuthTypes.BEARER, "USER")); }
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); }
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; }
/// <summary> /// /// </summary> /// <param name="config"></param> 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 } ); config.Formatters.Clear(); config.Formatters.Add(new XmlMediaTypeFormatter()); config.Formatters.Add(new JsonMediaTypeFormatter()); config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter()); // To make sure Chrome and Firefox allow for using apis directly. config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); }
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; }
public static void Register(HttpConfiguration config) { // Web API configuration and services var cors = new EnableCorsAttribute("http://localhost:9005", "*", "*"); config.EnableCors(cors); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "Row", routeTemplate: "api/rows/{number}", defaults: new { controller = "rows", number = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }
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 } //); }
public static HttpConfiguration Register() { var config = new HttpConfiguration(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(name: "DefaultRouting", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); config.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); config.Formatters.JsonFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/json-patch+json")); config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.MessageHandlers.Add(new CacheCow.Server.CachingHandler(config)); var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); return config; }
public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); //config.Routes.MapHttpRoute( // name: "DefaultApi", // //routeTemplate: "api/{controller}/{id}", // defaults: new { id = RouteParameter.Optional } //); var corsAttr = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(corsAttr); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}" ); var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); }
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( name: "Guess", routeTemplate: "api/games/{id}/guess", defaults: new { controller = "Guess", } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional} ); var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); }
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)); //Enable CORS //var cors = new EnableCorsAttribute("http://localhost/MonfuSportsStore", "*", "*"); var cors = new EnableCorsAttribute("http://localhost", "*", "*"); config.EnableCors(cors); //return JSON config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }
public static void Register(HttpConfiguration config) { // Web API configuration and services var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); }
public static void Register(HttpConfiguration config) { // Web API configuration and services // 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; var corsAttr = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"); config.EnableCors(corsAttr); }
public void GetCorsPolicyProvider_Preflight_DoesNotUseRouteDataOnTheRequest() { AttributeBasedPolicyProviderFactory providerFactory = new AttributeBasedPolicyProviderFactory(); HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Options, "http://localhost/sample" ); request.Headers.Add("Origin", "http://localhost"); request.Headers.Add(CorsConstants.AccessControlRequestMethod, "Put"); HttpConfiguration config = new HttpConfiguration(); request.SetConfiguration(config); var route = config.Routes.MapHttpRoute( "default", "{controller}/{id}", new { id = RouteParameter.Optional } ); request.SetRouteData( new HttpRouteData( route, new HttpRouteValueDictionary( new { action = "Options", controller = "sample", id = 2 } ) ) ); ICorsPolicyProvider provider = providerFactory.GetCorsPolicyProvider(request); Assert.True(request.GetCorsRequestContext().IsPreflight); EnableCorsAttribute enableCorsAttribute = Assert.IsType <EnableCorsAttribute>(provider); Assert.Equal(2, enableCorsAttribute.Origins.Count()); Assert.Equal("http://example.com", enableCorsAttribute.Origins[0]); Assert.Equal("http://localhost", enableCorsAttribute.Origins[1]); }
public static void Register(HttpConfiguration config) { var corsAttr = new System.Web.Http.Cors.EnableCorsAttribute("https://localhost:44319/", "*", "*"); config.EnableCors(corsAttr); }
/// <summary> /// Initializes a new instance of the <see cref="EnableCorsAttribute" /> class. /// </summary> /// <param name="origins">Comma-separated list of origins that are allowed to access the resource. Use "*" to allow all.</param> /// <param name="headers">Comma-separated list of headers that are supported by the resource. Use "*" to allow all. Use null or empty string to allow none.</param> /// <param name="methods">Comma-separated list of methods that are supported by the resource. Use "*" to allow all. Use null or empty string to allow none.</param> /// <param name="exposedHeaders">Comma-separated list of headers that the resource might use and can be exposed. Use null or empty string to expose none.</param> public EnableCorsAttribute(string origins, string headers, string methods, string exposedHeaders) { _internalAttribute = new System.Web.Http.Cors.EnableCorsAttribute(origins, headers, methods, exposedHeaders); }