首页 > 解决方案 > 在 Visual Studio 中调试 ASP.NET Core 应用程序

问题描述

我在我的解决方案中创建了一个新项目。该项目是 ASP.NET Core 应用程序。

问题是,当我单击 IISExpressiis 快速午餐按钮开始调试时,出现此错误:

错误

这些是项目属性Debug 财产1

财产2

我不知道该怎么办...有人可以帮助我吗?


编辑这是我的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"
        }
       }
     }

和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();
        });
     }
  }
}

这是我的控制器的片段(但后端永远不会在这里停止)

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

}

标签: asp.net-coreiis-express

解决方案


您的问题有点不清楚您到底要完成什么。但我会尽力澄清。

在您launchSettings.json指定的启动 URL 应该是backend

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

这意味着当应用程序启动时,它将从路径开始{base url}/backend(在您的情况下是base urlis http://localhost:8083)。因为您还设置了launchBrowserto true,所以浏览器也会在应用程序启动时打开。当浏览器打开一个路径时,它会向该路径发送一个 HTTP GET 请求。因此,当您启动应用程序时,浏览器会向路由发送 GET 请求{base url}/backend,但您没有控制器处理该路由,因此 HTTP 状态代码为 404。

正如亚当文森特还提到的那样,您还应该UsePathBase()在您的Startup.cs. 根据文档

添加一个中间件,该中间件从请求路径中提取指定的路径库并将其附加到请求路径库中。

我不相信这会让你更接近你想要完成的事情。

由于您的guestAuth()方法AuthController是 HTTP POST 类型,因此无法从浏览器访问该端点。我建议使用Postman或类似软件来开发和测试您的 API。如果您在guestAuth()方法中设置断点,启动应用程序进行调试,然后从 Postman 向 发送 POST 请求{base url}/api/Auth/test/guest,您应该点击该断点。


推荐阅读