I run my solution in webApi net.core, and angular , and iis. I success to pass login function(get). but update function (post), I get error:
Access to XMLHttpRequest at 'http://localhost:8080/api/Test/UpdateId' from origin
'http://localhost:3400' has been blocked by CORS policy: Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
localhost:8080 - i get from IIS server. localhost:3400 - i get from angular.
my controller:
[TypeFilter(typeof(PermissionFilter), Arguments = new object[] { Modules.Employees, Permissions.Edit, Permissions.Add })]
[HttpPost]
[Route("api/Test/UpdateId")]
public ActionResult<IdModel> UpdateId([FromBody]IdModel idModel)
{ //some code...
}
Startup.cs:
"AllowSpecificOrigin": "http://localhost:3400"
public void ConfigureServices(IServiceCollection services) {
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins(Configuration.GetValue<string>("AllowSpecificOrigin"))
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseCors("AllowSpecificOrigin");
app.UseDeveloperExceptionPage();
app.Use((corsContext, next) =>
{
corsContext.Response.Headers["Access-Control-Allow-Origin"] = "*";
return next.Invoke();
});
app.UseExceptionHandler(a => a.Run(async context =>
{
var feature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = feature.Error;
var result = JsonConvert.SerializeObject(new { hasError = true, message = exception.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = 200;
await context.Response.WriteAsync(result);
}));
app.UseSession();
app.UseMvc();
}
else
{
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseExceptionHandler(a => a.Run(async context =>
{
var feature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = feature.Error;
var result = JsonConvert.SerializeObject(new { hasError = true, message = exception.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = 200;
await context.Response.WriteAsync(result);
}));
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
});
}
my angular, and run : npx ng serve --open --port 3400
UpdateEmployee(idModel): any {
return this._http.post('http://localhost:8080/api/Test/UpdateId', idModel);
}
how fix this error? maybe it is IIS?
thank.