【问题标题】:ASP.NET Core: Web API not served over IIS Express - why?ASP.NET Core:不通过 IIS Express 提供 Web API - 为什么?
【发布时间】: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


【解决方案1】:

您不能只绑定任何 url 到 applicationUrl 属性,只能绑定 3 种类型的 url

 1. {scheme}://{loopbackAddress}:{port} (e.g. http://localhost:3000)
 2. {scheme}://{IPAddress}:{port} (e.g. http://192.168.2.130:3000)
 3. {scheme}://*:{port} (e.g. http://*:3000)

虽然端口是可选的,但如果没有提到,http 的默认 80 和 https 的 443 是绑定的

如果您的要求是默认启动控制器, 您可以将 applicationUrl 保留为基本 url,并在其中添加带有控制器名称的 launchUrl 属性

【讨论】:

  • 这应该改变什么?这不是我的问题的答案。
  • 已更新,立即查看。我以为你会明白的。
  • 好主意。但正如我之前写的:没有区别。
  • 它仍然不适合您?它应该工作伙伴。我们可以实时分享吗?
  • 当然。那很好啊。如何联系您了解详情?
猜你喜欢
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
相关资源
最近更新 更多