我正在使用 .net core 来公开 API。当我从 postman 调用 api 时,某些方法未命中,收到 404 未找到错误消息。
[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' 时,收到未找到错误消息。
但是当尝试时,
[HttpPost]
public async Task<bool> Post()
{
return true;
}
它将返回真实值。
但我注意到,当我调用 localhost 'AddLogs' api 时工作正常。它将命中 api。但是当我在 azure 中发布时,它显示我没有找到。
最佳答案
我在我的网站上进行了测试,效果很好。
原因是部署或默认的 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?}");
});
关于azure - Api Post 方法在.net core azure 生产中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146140/