asp.net-core - 在 Visual Studio 中调试 ASP.NET Core 应用程序
问题描述
我在我的解决方案中创建了一个新项目。该项目是 ASP.NET Core 应用程序。
问题是,当我单击 IISExpress开始调试时,出现此错误:
我不知道该怎么办...有人可以帮助我吗?
编辑这是我的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);
}
}
解决方案
您的问题有点不清楚您到底要完成什么。但我会尽力澄清。
在您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 url
is http://localhost:8083
)。因为您还设置了launchBrowser
to 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
,您应该点击该断点。
推荐阅读
- matrix - Power BI 矩阵 - 使用 DAX 减去两个(动态)对应列
- android-5.1.1-lollipop - 在 Delphi 10.4 上为 Android 5.1 开发?
- node.js - 如何从 kubernetes 应用程序在 kubernetes 中正确运行作业
- mongodb - docker容器内的mongod进程如何允许来自外部的连接
- laravel - 发送电子邮件请求时如何更改默认的 Laravel 8 Fortify 已发送电子邮件?
- angular - ngrx 中的不可变状态有问题
- java - 使用 switch 语句时如何将用户返回到主菜单?爪哇
- c# - 在 C# 中将 SQL 转换为 LINQ
- reactjs - 如何在 React 中单击输入时清除文本区域?
- flutter - 我如何实时查看下拉按钮