Кажется, я не могу отладить свой ASP. NET Core Web API с помощью IIS Express.
Я всегда получаю ответ 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 справа:
Please note the HTTP status codes returned.
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();
...