在Visual Studio中调试ASP.NET Core应用程序

3
我在我的解决方案中创建了一个新项目。该项目是ASP.NET Core应用程序。
问题是,当我点击IISExpress启动调试时,出现了以下错误:

error

这些是项目属性 Debug property1

property2

我不知道该怎么办...有人可以帮帮我吗?
编辑 这是我的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);
    }

}

请尝试在URL中不使用“/backend”。 - Darshani Jayasekara
无论您创建了什么类型的ASP.NET Core项目,它都不可能处理http://localhost:8083/backend。因此,预计会出现404错误。您需要学习路由并了解此Web应用程序所期望的正确URL。 - Lex Li
我使用了与你相同的Debug配置,但是在我的端上没有异常。你能提供一下与你的控制器相关的代码吗? - Tupac
我有超过1个控制器。这不像是其他配置文件吗? - Martina
我在你的 launchSettings.json 文件中看到了后缀 backendCore,要启动的路由后缀是 backend 还是 backendCore - Antonio Leonardo
显示剩余2条评论
4个回答

1
修改你的startup.cs文件中的Configure方法中的以下代码。
if (env.IsDevelopment())
{
    app.UsePathBase("/backend"); //Add this line
    app.UseDeveloperExceptionPage();
}

这将添加一个中间件,从请求路径中提取指定的路径基础,并将其附加到请求路径基础。这仅适用于开发环境,如果您的生产环境也配置了 backend 子文件夹,请将其放在 if 条件之外。

请编辑您的问题,并从launchSettings.jsonstartup.cs文件中添加代码片段。您可以在Properties文件夹中找到launchSettings.json文件。 - prinkpan

1
改变你的代码。希望能在你的情况下工作!
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();

    }

1

根据您的问题,很难确定您想要实现什么。但我会尽力澄清一些事情。

在您的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 urlhttp://localhost:8083)。由于您还将launchBrowser设置为true,因此应用程序启动时浏览器也会打开。当浏览器打开一个路径时,它会向该路径发送HTTP GET请求。因此,当您启动应用程序时,浏览器会向{base url}/backend路由发送GET请求,但是您没有控制器处理该路由,因此HTTP状态代码为404。

正如Adam Vincent所提到的,您还应该在Startup.cs中删除对UsePathBase()的调用。根据docs的说明:

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

我不认为这会让您更接近您想要实现的目标。

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


1

HTTP POST vs GET

HTTP POST和GET的区别在于,你的终点是[HttpPost],不能在浏览器中输入URL并导航到该URL。在浏览器中导航到URL是一个GET请求。

路由

你分享了一个控制器和终点的(部分)代码,如下:

[Route("api/Auth/{language}/guest")]
完全匹配:http://localhost:8083/api/Auth/{language}/guest

导航到http://localhost:8083/backend会导致404错误,因为它没有精确匹配路径。路由不涵盖路径的任何部分。

UsePathBase

我不确定你试图使用UsePathBase来完成什么,这只有在代理场景下才需要(例如:使用nginx或Apache进行托管),你没有提到任何要求需要使用它,所以我建议删除它,因为它只会使事情变得更加复杂。

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

我相信这将会引发更多问题,所以请适当编辑您的帖子并提出问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接