milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. ASP.NET

Enable Cross-Origin Requests (CORS) in ASP.NET Core

Details
Written by: Stanko Milosev
Category: Core
Published: 24 January 2020
Last Updated: 24 January 2020
Hits: 1268
Title of this article I have took from here.

If you are receiving a message like:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://test:2021/api/values. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
This means that you have to enable CORS (since 2014)

In my case I have created new ASP.NET Core WebApi on the link http://test:2021/api/values, and I was accessing that link from http://gallery.milosev.com:9090.

Open Startup.cs:

First add line:

readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
Then:
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

  services.AddCors(options =>
  {
	options.AddPolicy(MyAllowSpecificOrigins,
	builder =>
	{
	  builder.WithOrigins("http://gallery.milosev.com:9090");
	});
  });
}
Here notice: http://gallery.milosev.com:9090, and at the end:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  if (env.IsDevelopment())
  {
	app.UseDeveloperExceptionPage();
  }
  else
  {
	app.UseHsts();
  }

  app.UseCors(MyAllowSpecificOrigins);
  app.UseHttpsRedirection();
  app.UseMvc();
}
Whole Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace EnableCORS
{
  public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

      services.AddCors(options =>
      {
        options.AddPolicy(MyAllowSpecificOrigins,
        builder =>
        {
          builder.WithOrigins("http://gallery.milosev.com:9090");
        });
      });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseHsts();
      }

      app.UseCors(MyAllowSpecificOrigins);
      app.UseHttpsRedirection();
      app.UseMvc();
    }
  }
}

Debug Asp.Net core on IIS by attaching to process

Details
Written by: Stanko Milosev
Category: Core
Published: 01 January 2020
Last Updated: 06 April 2022
Hits: 1199
Here I explained how to publish code to IIS, and here I explained one way to debug your application, but I don't like it, since it is taking too much time to start. Here I have found another way to debug asp.net core application, from my point of view much better. Here I already explained hot to attach to process, the only difference ist that you have to attach to "dotnet.exe":

Debug Asp.Net core on IIS in VS2017

Details
Written by: Stanko Milosev
Category: Core
Published: 31 December 2019
Last Updated: 31 December 2019
Hits: 1411
To write this article I was using Development-time IIS support in Visual Studio for ASP.NET Core.

First in Visual Studio installer enable development-time IIS support in Visual Studio:

Create new Asp.Net Core MVC application.

Add application to IIS, as physical path in my case I added "C:\Users\pera\Documents\Visual Studio 2017\Projects\WebApplication1\WebApplication1":

In Visual Studio right click on project go to properties:

Go to Debug, click on new:

In profile name write IIS:

Launch: IIS, Select the check box for Launch browser: http://test:2021/, the Environment variables section, select the Add button. Provide an environment variable with a Name of ASPNETCORE_ENVIRONMENT and a Value of Development:

Restart Visual Studio as administrator, if you haven't already.

Set the Start Debugging button to the IIS profile and select the button to start the app:

Publish Asp.Net core to IIS

Details
Written by: Stanko Milosev
Category: Core
Published: 31 December 2019
Last Updated: 31 December 2019
Hits: 1477
If you simply try something like I already explained here, it will not work, you will receive the error:

Server Error in '/' Application.

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: An error occurred loading a configuration file: Failed to start monitoring changes to 'C:\Users\pera\Documents\Visual Studio 2017\Projects\WebApplication1\WebApplication1' because access is denied.

Source Error:

[No relevant source lines]

Source File: C:\Users\pera\Documents\Visual Studio 2017\Projects\WebApplication1\WebApplication1\web.config Line: 0

In the article "Host ASP.NET Core on Windows with IIS" it is explained how to publish Asp.Net core to IIS. Here are my short notes.

First install .NET Core Hosting Bundle. If you simply publish web site without installation you will receive error:

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

Detailed Error Information:
Module IIS Web Core
Notification Unknown
Handler Not yet determined
Error Code 0x8007000d
Config Error
Config File \\?\C:\projects\dotNet\asp.net\core\web.config
Requested URL http://test:2020/
Physical Path
Logon Method Not yet determined
Logon User Not yet determined

Config Source:
-1:
0:

More Information:
This error occurs when there is a problem reading the configuration file for the Web server or Web application. In some cases, the event logs may contain more information about what caused this error.
View more information »

If you receive error like:

HTTP Error 500.21 - Internal Server Error

Handler "aspNetCore" has a bad module "AspNetCoreModule" in its module list

Then you have to change AspNetCoreModule in IIS.

Open Modules in IIS:

Either add AspNetCoreModuleV2 with code %ProgramFiles%\IIS\Asp.Net Core Module\V2\aspnetcorev2.dll or change existing one:

Then go to "Handler Mappings", and change aspNetCore to "AspNetCoreModuleV2":

Subcategories

MVC 4

MVC 3

WebApi

Core

Page 11 of 11

  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11