【问题标题】:Asp core published API routing issueasp核心发布API路由问题
【发布时间】:2020-05-14 01:31:59
【问题描述】:

我在 ASP Core(分别为 2.2 和 3.0)中有一个 MVC 应用程序和一个 API,它们在 IIS 10 下的 2 个不同服务器上发布。Web 应用程序运行良好,但我对 API 有一个意外问题:路由坏了。

这是我用来从邮政编码(codePostal)获取城市的示例方法:

[Authorize]
[ApiController]
[Route("[controller]")]
public class CommunesController : ControllerBase
{
    private readonly ICommuneService _communeService;

    public CommunesController(ICommuneService communeService)
    {
        _communeService = communeService;
    }

    [AllowAnonymous]
    [HttpGet("{codePostal}")]
    public IActionResult GetCommunesFromCP(string codePostal)
    {
        IList<CommuneDto> resCommunes = _communeService.GetCommunesFromCP(codePostal);

        if (resCommunes == null || resCommunes.Count == 0)
            return NotFound();
        else
            return Ok(resCommunes);
    }
}

如果我使用 url https://mydomain.fr/communes/38000 调用此方法,我会收到 404 错误。如果我使用经典的查询字符串语法https://mydomain.fr/communes?codePostal=38000,它可以工作!我不明白为什么会这样。此外,当我在控制器中有几个带有 RESTful 命名的 GET 方法时,会调用错误的方法,因为路由丢失了!一切都在调试中运行良好,但自从我部署我的 API 以来,我已经为此苦苦挣扎了好几个小时。

这是我在 Startup.cs 中的 API 配置方法,如果有帮助的话:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();

    // global cors policy
    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

    app.UseRouting();

    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

【问题讨论】:

  • 愿意打赌控制器正在拦截您收到的请求并将您的参数 3800 转换为整数而不是字符串,并且在进行路由时找不到正确的路由.. 你仍然得到一个404 什么时候给端点传递一个明显的字符串?
  • @TravisActon 是的,我做了,我做了测试。问题比这更大:在控制器中,如果我在同一个端点上有两个 GET 方法,一个有参数,一个没有,我总是被路由到没有参数的那个,因为参数的路由没有正确完成。

标签: c# iis asp.net-mvc-routing asp.net-core-webapi


【解决方案1】:

我终于找到了问题:Startup.cs中app.UseAuthentication()的位置不对。我将它移到 app.UseCors(...) 之前,并且在我重新发布 API 后路由工作正常。我没有看到 app.UseAuthentication() 的位置与问题之间的联系,因为在这两种情况下它都在 app.UseRouting() 之前:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();

    // global cors policy
    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

    app.UseRouting();

    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

【讨论】:

    【解决方案2】:

    您需要在您的方法之前添加[Route("GetCommunesFromCP/{codePostal}")],在您更改后的方法下方:

       [AllowAnonymous]
       [Route("GetCommunesFromCP/{codePostal}")]
        public IActionResult GetCommunesFromCP(string codePostal)
        {
            IList<CommuneDto> resCommunes = _communeService.GetCommunesFromCP(codePostal);
    
            if (resCommunes == null || resCommunes.Count == 0)
                return NotFound();
            else
                return Ok(resCommunes);
        }
    

    【讨论】:

    • 感谢您的回答,但这是一个宁静的 API,我故意没有公开方法名称,它是 HTTP 动词和将提供路由信息的参数。
    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 2021-07-20
    • 2021-05-29
    • 2020-11-27
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多