【发布时间】:2020-08-02 12:35:49
【问题描述】:
我似乎无法使用 IIS Express 调试我的 ASP.NET Core Web API。
我总是收到404 File not found 回复。
当我使用 Kestrel 进行调试时,一切正常。
为什么 IIS Express 不工作?
这是我的launchSettings.json 文件:
{
"iisSettings":
{
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress":
{
"applicationUrl": "http://localhost:52772/Users",
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles":
{
"IIS Express":
{
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables":
{
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebAPI":
{
"commandName": "Project",
"launchBrowser": true,
"environmentVariables":
{
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
编辑
这是描述这两种情况的屏幕截图:左侧是 Kestrel,右侧是 IIS Express:
请注意返回的 HTTP 状态代码。
public static void Main(string[] args) =>
Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<WebConfiguration>())
.Build()
.Run();
public class WebConfiguration
{
private readonly IConfiguration _configuration;
public WebConfiguration(IConfiguration configuration) => _configuration = configuration;
// This method gets called by the runtime once. Use this method to add services to the container for dependency injection.
public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton(_configuration.GetSection("AppSettings").Get<Options>())
.AddScoped(typeof(IUserAccess), typeof(UserAccess))
.AddScoped(typeof(ITaskItemAccess), typeof(TaskItemAccess))
.AddScoped(typeof(IGenderAccess), typeof(GenderAccess))
.AddSwaggerGen()
.AddControllers()
;
}
// This method gets called by the runtime once. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Options options)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app
.UseHttpsRedirection()
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints => endpoints.MapControllers())
.UseSwagger()
;
if (env.IsDevelopment()) app.UseSwaggerUI();
}
}
namespace WebAPI.Controllers
{
[Route("[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUserAccess _repository;
public UsersController(IUserAccess repository) => _repository = repository;
[HttpGet]
public ICollection<User> Get() => _repository.Get();
...
【问题讨论】:
-
它是否有任何用户控制器或您是否设置了默认路由。只需尝试从链接localhost:52772 中删除用户并尝试
-
回答您的问题:是的,确实如此。我意识到我的问题不清楚。我现在添加了一个屏幕截图,描述了端点存在并且在 Kestrel 下按预期工作,但在 IIS Express 下没有。
-
能否也提供startup.cs和usercontroller.cs类的configure方法中的代码?
-
你能不能打开开发者异常页面,给我们看看实际的异常情况。
-
我现在添加了缺少的代码部分。 @DurgaPrasad:开发人员异常页面已启用。也不例外。 HTTP
404表示没有可调用的代码。甚至没有触发 ASP.NET。所以这不是我的代码的问题,但 IIS Express 有问题。
标签: asp.net-core asp.net-core-webapi iis-express