【发布时间】:2019-02-15 10:20:11
【问题描述】:
我在 C# 控制器中设置了一个异步任务,如下所示:
命名空间 MyApp.Api {
public class TimeAllocationController : BaseApiController
{
[Route("api/timeallocation")]
[HttpPost]
public async Task<ActivityValidationResult> Post(string id)
{
//logic here...
}
理想情况下,我想使用 $.post() 方法在 JQuery 中传递整个有效负载,但我不断收到 405 method not allowed if
我尝试在有效负载中传递 C# 的 Post() 字符串 ID。我只能这样传递:
$.post('/api/timeallocation/' + categoryId...
我不能像这样传递它:
$.post('/api/timeallocation?id=' + categoryId...
上述两个我都不想做,只需在 JS 文件中设置一个带有 id 和所有其他必需属性的 payload 变量,然后调用 $.post() 即可。
至于出现405错误的其他可能原因,我已经验证不是因为身份验证的原因。
这里有什么我忽略的地方吗?
【问题讨论】:
-
您需要将路由配置更改为
[Route("api/timeallocation/{id}")]并执行$.post('/api/timeallocation/' + categoryId以从Ajax 调用
标签: c# jquery .net post .net-4.6