【问题标题】:Api Post method not working in .net core azure productionApi Post 方法在 .net core azure 生产中不起作用
【发布时间】:2019-05-16 17:58:12
【问题描述】:

我正在使用 .net 核心来公开 API。当我从邮递员调用 api 时,某些方法没有命中,得到 404 not found 错误消息。

  [HttpPost]
    public async Task<bool> AddLogs([FromBody]List<LogModel> model)
    {
        var result = false;
        foreach (var item in model)
        {
            result = await _logService.Insert("Logs", item);
        }

        return result;
    }

   public class LogModel: TableEntity
{
    public int Status { get; set; }
    public bool IsBreak { get; set; }
    public string Location { get; set; }
    public DateTime StartDateAndTime { get; set; }
    public DateTime EndDateAndTime { get; set; }
    public string Remarks { get; set; }
    public int Id { get; set; }
  }

当我调用 api 'AddLogs' 时,得到 not found 错误消息。

但是当尝试时,

 [HttpPost]
    public async Task<bool> Post()
    {
        return true;
    }

它会返回真值。

但我注意到当我调用 localhost 'AddLogs' api 时工作正常。它将命中 api。但是当我在 azure 中发布时,它显示我找不到。

【问题讨论】:

  • 乔伊的回答是一个很好的起点。可能是路由问题。另一种选择是,在调用 Post 时,您提交的是 API 模型无法识别的正文。即,如果它是 List,请确保 body 是 Json/app 并且它看起来像 [{...}]。按此顺序排列数组括号和对象括号。
  • 是的,我终于找到了问题,这是由于日期时间。

标签: azure api asp.net-web-api asp.net-core azure-web-app-service


【解决方案1】:

我在我的网站上进行了测试,效果很好。

这是因为部署或默认的 ASP.NET Core Web API 模板在网站的根目录中不包含默认文档。例如 index.htm、defualt.aspx、default.htm 是默认文件,如果访问 URL 时没有提供特定文件,IIS 会下发。

如果您有多个 httppost 方法,您可以设置 [HttpPost("AddLogs/")] 来指定 AddLogs 操作。记住还要在Configure方法中添加以下代码。

app.UseMvc(routes =>
{
    routes.MapRoute(name: "default", template: "api/{controller}/{action}/{id?}");
});

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2020-02-02
    • 2022-08-21
    • 2020-06-11
    • 2018-02-16
    • 1970-01-01
    • 2018-04-28
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多