Answers for "asp.net web api enable cors"

C#
2

asp.net allow cors for all

// Credit_1: https://www.infoworld.com/article/3327562/how-to-enable-cors-in-aspnet-core.html
// Credit_2: https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0

// At class "Startup":
#region global variables
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
#endregion

// Inside "ConfigureServices" function
// From a --SINGLE SOURCE-- (in this case localhost:4200):
services.AddCors(options =>
{
	options.AddPolicy(name: MyAllowSpecificOrigins,
	builder => builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader());
});

// From --ALL SOURCES--:
services.AddCors(options =>
{
	options.AddPolicy(name: MyAllowSpecificOrigins,
    builder => builder.AllowAnyOrigin());
});

// Inside "Configure" function
app.UseCors(MyAllowSpecificOrigins);
Posted by: Guest on August-03-2021
0

how to enable cors policy in web api

BY LOVE
To enable CORS policy in web api, You need to add this method in your Global.asax file of API project. i.e
        
        protected void Application_BeginRequest()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        }
Posted by: Guest on July-04-2020
0

enable cors asp.net mvc

Response.AppendHeader("Access-Control-Allow-Origin", "*");
Posted by: Guest on October-04-2020

Code answers related to "asp.net web api enable cors"

C# Answers by Framework

Browse Popular Code Answers by Language