0

I'm developing an ASP.NET Core-based web app, but I'm currently in the debugging phase. This is also my very first time working with .NET Core.

I want to do application-level debugging and testing by making requests to the developed ASP.NET Core Web API from the frontend.

Can I publish the API somewhere for free for now to debug and test the entire web app?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rafay Shams
  • 13
  • 1
  • 5
  • is it in the same solution? If it is you can run multiple projects in debug at the same time. Just use localhost: for the reference to the API. This allows you to set breakpoints within either project. If not in the same solution, then run them in separate VS instances. – Scott Mildenberger Oct 11 '22 at 17:19
  • Yes, it's all part of the same solution. I have an ASP.NET front-end, a class library that that has a public method to calculate certain things based on the passed in parameters. The web API that has the class library as its reference. The ASP.NET front-end makes HTTP GET requests to the API; the API calls the public method and returns the result. – Rafay Shams Oct 11 '22 at 22:22
  • Now I'm getting this error: localhost/:1 Access to XMLHttpRequest at 'localhost:?fint=4&sint=6' from origin 'https://localhost:' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted. – Rafay Shams Oct 11 '22 at 22:29
  • Something like this https://stackoverflow.com/questions/57530680/enable-cors-for-any-port-on-localhost should take care of the CORS issue when using localhost. It might be different depending what version of .Net you are using, google brings up lots of solutions. – Scott Mildenberger Oct 12 '22 at 13:23

1 Answers1

0

Can I publish the API somewhere for free for now to debug and test the entire web app?

You don't need to publish your API to test from your frontend app. You can can do that running your Web Api project locally therefore, setting debugger pointer to any break point.

Example Debugging Using Swagger In Localhost:

You could have a look on following example, I am testing the Web API project running on local environment using Swagger. I am sending request to my desired API and debugging at the same time whether the response expected. See below screenshot:

enter image description here

What if you encounter "Origin 'localhost:<port>' has been blocked by CORS policy" Error :

You could simply handel above error by following ways:

If you want to allow all request Endpoint:

        builder.UseCors(x => x
       .AllowAnyMethod()
       .AllowAnyHeader()
       .SetIsOriginAllowed(origin => true) // allow any origin

Note: Above code resolve your CORS error and allow all coming request from your frontend app

If you want to allow Certain request Endpoint:

var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

var builder = WebApplication.CreateBuilder(args);


builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      policy =>
                      {
                          policy.WithOrigins("http://localhost:5094");
                      });
});

Then use as below:

app.UseCors(MyAllowSpecificOrigins);

Note: Above code allow the request only from the endpoint containing http://localhost:5094. URL

If you sitll need further information you could refer to our official document here

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43