debug ASP.NET Core application in Visual Studio

Viewed 495

I created a new project in my solution. The project is ASP.NET Core application.

The problem is that When I click on IISExpress iis express lounch button to start debugging I have this error:

error

These are the project property Debug property1

property2

I don't know what to do... Can someone help me?


EDIT This is my launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iis": {
       "applicationUrl": "http://localhost:8083/backendCore",
       "sslPort": 0
     },
    "iisExpress": {
       "applicationUrl": "http://localhost:8083",
       "sslPort": 0
     }
 },
 "$schema": "http://json.schemastore.org/launchsettings.json",
 "profiles": {
     "IIS Express": {
       "commandName": "IISExpress",
       "launchBrowser": true,
       "launchUrl": "backend",
       "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
       }
     },
     "backendCore": {
        "commandName": "Project",
        "launchBrowser": true,
        "launchUrl": "weatherforecast",
        "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "https://localhost:5001;http://localhost:5000"
       },
       "prova": {
          "commandName": "IIS",
          "launchBrowser": true,
          "launchUrl": "backend"
        }
       }
     }

and startup.cs

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

     public IConfiguration Configuration { get; }

     // This method gets called by the runtime. Use this method to add services to the container.
     public void ConfigureServices(IServiceCollection services)
     {
        services.AddControllers();
     }

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
        if (env.IsDevelopment())
        {
           app.UsePathBase("/backend"); //Add this line
           app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
           endpoints.MapControllers();
        });
     }
  }
}

this is the snippet of my controller (but the backend never stops here)

namespace backendCore.Controllers
{
public class AuthController : ControllerBase
{
    [Route("api/Auth/{language}/guest")]
    [HttpPost]
    
    public ActionResult guestAuth(string language)
    {
       return Ok(true);
    }

}
4 Answers

Change the following code in your startup.cs file Configure method

if (env.IsDevelopment())
{
    app.UsePathBase("/backend"); //Add this line
    app.UseDeveloperExceptionPage();
}

This adds a middleware that extracts the specified path base from a request path and postpend it to the request path base. This is only for the development environment, if your production environment also has a backend sub-folder configured, put it outside the if condition.

Change your code. Hope work in your case!

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddMvc(options =>
            {
                options.EnableEndpointRouting = false;
            });
        }



 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UsePathBase("/backend"); //Add this line
            app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();
        app.UseMvcWithDefaultRoute();

    }

HTTP POST vs GET

Your endpoint is a [HttpPost], you can not enter the URL in a browser and navigate to it. Navigating to a URL in the browser is a GET request.

Routing

You have shared the (partial) code for a controller and endpoint which is the following:

[Route("api/Auth/{language}/guest")]
Matches EXACTLY: http://localhost:8083/api/Auth/{language}/guest

Navigating to http://localhost:8083/backend will result in a 404, since it's not matching the exact path. Routing does not cover parts of the path.

UsePathBase

I'm not sure what you are trying to accomplish with UsePathBase, that should only be necessary when working in a proxy scenario (eg; hosting with nginx or Apache), you haven't alluded to any requirement that would make using it necessary, so I would remove it, since it's just muddying the waters.

For reference: https://www.hanselman.com/blog/dealing-with-application-base-urls-and-razor-link-generation-while-hosting-aspnet-web-apps-behind-reverse-proxies

I'm sure this will generate more questions, so please edit your post appropriately and ask away.

It is a bit unclear from your question exactly what you are trying to accomplish. But I'll try and bring some clarity.

In your launchSettings.json you specify that the launch URL should be backend:

"profiles": {
     "IIS Express": {
       "commandName": "IISExpress",
       "launchBrowser": true,
       "launchUrl": "backend",  // <----- Here you specify the launch URL
       "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
       }
     },

This means that when the application starts, it will start at the path {base url}/backend (in your case the base url is http://localhost:8083). Because you have also set the launchBrowser to true, the browser will also open when the application starts. When a browser opens a path it will send a HTTP GET request to that path. So when you start your application, the browser sends a GET request to the {base url}/backend route, but you have no controller handling that route, hence the HTTP Status code 404.

As Adam Vincent also mentions, you should also remove the call to UsePathBase() in your Startup.cs. According to the docs it:

Adds a middleware that extracts the specified path base from request path and postpend it to the request path base.

I don't believe this brings you closer to what you are trying to accomplish.

Since your guestAuth() method in your AuthController is of a HTTP POST type, it will not be possible to hit that endpoint from a browser. I would suggest using Postman or similar software to develop and test your API. If you set a breakpoint in your guestAuth() method, start your application with debugging and then send a POST request from Postman to {base url}/api/Auth/test/guest, you should hit that breakpoint.

Related