4

Working on an ASP.net core 2.1 web API project. I need to enable the API so that it can be accesed by client applications that we also have under developement.

So far, the only way I've found to publish to IIS is by doing a manual process:

  • Run dotnet publish -c Release
  • Copy the files in bin\Release\netcoreapp2.1\publish\ to my IIS Web App folder

I wonder if there is a more straight forward way of doing this.

Also It takes quite sometime to build this release, so for a development environment it's quite a slow process. The problem is that we cannot allow external access to the WEB api when running with F5 on the Integrated test server. How can we enable an more agile testing environment?

Another issue is that when calling for example fetch('MyAPIServer/api/MyItems') from a javascript application, I get a CORS error:

Failed to load http://localhost:86/api/shit: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8082' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled

Is enabling CORS absolutely necesary when developing this type of apps?

If I fetch like this:

fetch(
    `http://localhost:86/api/shit`,{mode: 'no-cors'}
  )

I get:

Uncaught (in promise) SyntaxError: Unexpected end of input
at eval (Pos.vue?7f37:68)
Jack Casas
  • 914
  • 18
  • 37
  • https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input-when-using-mode-no-cors/43319482#43319482 – sideshowbarker Jul 30 '18 at 23:13
  • this question is my solution thanks. after i develop my dot net core webapi i forget to run dotnet publish -c Release thnks – saber tabatabaee yazdi Jan 05 '19 at 16:26
  • Let me add that we moved from VS Code to VS Community. IMO, it's worthed if deploying to IIS, as you can just publish from the GUI – Jack Casas Jan 06 '19 at 19:58

1 Answers1

0

As far as the CORs issue goes you can add the following to your startup:

public void ConfigureServices(IServiceCollection services)
{
    // Rest of config stuff ...
    services.AddCors();
}

Then in you will also need to add the following.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseCors(builder =>
            {
                builder.WithOrigins("http://localhost:8080",
                                    "http://localhost:8081", 
                                    "http://localhost:8082")   
                       .AllowAnyMethod()
                       .AllowAnyHeader()
                       .AllowCredentials();
                });
                app.UseMvc();
            }
rahicks
  • 573
  • 7
  • 19