首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Visual Studio中调试ASP.NET核心应用程序

在Visual Studio中调试ASP.NET核心应用程序
EN

Stack Overflow用户
提问于 2021-07-04 16:19:43
回答 4查看 211关注 0票数 3

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

问题是当我点击IISExpress时

要开始调试,我会遇到以下错误:

这些是项目属性Debug

我不知道该怎么办..。有人能帮我吗?

编辑这是我的launchSettings.json

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
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();
        });
     }
  }
}

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

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

}
EN

回答 4

Stack Overflow用户

发布于 2021-07-06 20:20:23

更改startup.cs文件Configure方法中的以下代码

代码语言:javascript
复制
if (env.IsDevelopment())
{
    app.UsePathBase("/backend"); //Add this line
    app.UseDeveloperExceptionPage();
}

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

票数 1
EN

Stack Overflow用户

发布于 2021-07-06 22:57:30

更改您的代码。希望能在你的案例中起作用!

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-07-07 01:17:04

HTTP POST vs GET

您的终结点是一个[HttpPost],您不能在浏览器中输入该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

我相信这会产生更多的问题,所以请适当地编辑你的帖子并询问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68242885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档