Enabling CORS through Web.config vs WebApiConfig and Controller attributes

If you add the headers to the web.config, every request that is served by that application will include the specified headers. This method is supported at the web server level and doesn’t depend on config.EnableCors() being executed. You can use that method to add any HTTP header you want. On the flip side, the EnableCors … Read more

Migrate Global.asax to Startup.cs

As you are already aware, OwinContext consumed by Startup.Configuration() is different from the traditional ASP.NET HttpContext consumed by MvcApplication.Application_Start(). Both are using different context pipelines. More specifically, ASP.NET MVC still relies on System.Web.dll while ASP.NET Web API doesn’t. Therefore, based on your code, some methods usually laid in MvcApplication.Application_Start() can’t be run within Startup.Configuration(): AreaRegistration.RegisterAllAreas();: … Read more

Enable CORS for Web Api 2 and OWIN token authentication

I know your issue was solved inside comments, but I believe is important to understand what was causing it and how to resolve this entire class of problems. Looking at your code I can see you are setting the Access-Control-Allow-Origin header more than once for the Token endpoint: app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); And inside GrantResourceOwnerCredentials method: context.OwinContext.Response.Headers.Add(“Access-Control-Allow-Origin”, new[] … Read more

How to use Swagger as Welcome Page of IAppBuilder in WebAPI

I got this working how I wanted by adding a route in RouteConfig.cs like so: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapHttpRoute( name: “swagger_root”, routeTemplate: “”, defaults: null, constraints: null, handler: new RedirectHandler((message => message.RequestUri.ToString()), “swagger”)); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } … Read more

Web API 2 – Implementing a PATCH

I hope this helps using Microsoft JsonPatchDocument: .Net Core 2.1 Patch Action into a Controller: [HttpPatch(“{id}”)] public IActionResult Patch(int id, [FromBody]JsonPatchDocument<Node> value) { try { //nodes collection is an in memory list of nodes for this example var result = nodes.FirstOrDefault(n => n.Id == id); if (result == null) { return BadRequest(); } value.ApplyTo(result, ModelState);//result … Read more