为什么两个相同的 REST 请求中的一个起作用而另一个不起作用?
Why does one of two identical REST requests work and not the other?
我有这两个小控制器:
[AllowAnonymous]
[RoutePrefix("api/Org")]
public class OrgController : BaseController
{
[HttpPost]
public async Task<IEnumerable<Organization>> Get()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Organizations.ToListAsync();
}
}
和
[AllowAnonymous]
[RoutePrefix("/api/Branch")]
public class BranchController : BaseController
{
[HttpPost]
public async Task<IEnumerable<Branch>> Get()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Branches.ToListAsync();
}
}
我这样称呼它们,分别使用 System.Net.Http.HttpClient
:
HttpResponseMessage response = await Client.PostAsync("/api/Org", null, cancellation);
和
HttpResponseMessage response = await Client.PostAsync("/api/Branch", null, cancellation);
当我请求 Orgs 时,我成功请求了 returns 4 个 Orgs,但是当我请求 Branches 时,我得到的响应是 HTTP 405 - Method not allowed.现在我知道我正在使用 POST 向 Get
方法发出请求,但很久以前我了解到它出于某种原因更安全,并且通常工作正常。
这里的要点是,这个经过验证的模式一直对我有效,并且对整个应用程序中的所有其他此类控制器和 POST 请求也有效。是什么导致 "/api/Branch"
的请求失败?
更新: 我将操作方法签名更改为如下所示,现在工作正常:
[HttpPost]
[Route("Get")]
public async Task<IEnumerable<Branch>> Fetch()
这很奇怪,因为 POST 请求直接作用于所有其他控制器上的 Get
操作,只要存在 HttpPost
属性。我的问题已经解决,但这个问题仍然悬而未决,为什么。与 Jinish 的回答相反,路由前缀开头的 /
似乎没有区别。有些控制器有,有些没有,但它们都能正常工作,除了 BranchController
.
我不相信你的 [RoutePrefix] 可以以 '/' 开头
应该是 [RoutePrefix("api/Branch")]
我使用的是 System.Web.Mvc.AllowAnonymousAttribute
而不是 System.Web.Http.AllowAnonymousAttribute
。使用现代便利设施,当我添加属性时,系统提示我(我认为是 ReSharper,但可能是 VS 本身)为它导入一个命名空间。在我匆忙中,我没有注意到它们是两个名称空间作为选项,我选择了错误的一个。更正如下:
using System.Collections.Generic;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web.Http;
//using System.Web.Mvc;
我有这两个小控制器:
[AllowAnonymous]
[RoutePrefix("api/Org")]
public class OrgController : BaseController
{
[HttpPost]
public async Task<IEnumerable<Organization>> Get()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Organizations.ToListAsync();
}
}
和
[AllowAnonymous]
[RoutePrefix("/api/Branch")]
public class BranchController : BaseController
{
[HttpPost]
public async Task<IEnumerable<Branch>> Get()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Branches.ToListAsync();
}
}
我这样称呼它们,分别使用 System.Net.Http.HttpClient
:
HttpResponseMessage response = await Client.PostAsync("/api/Org", null, cancellation);
和
HttpResponseMessage response = await Client.PostAsync("/api/Branch", null, cancellation);
当我请求 Orgs 时,我成功请求了 returns 4 个 Orgs,但是当我请求 Branches 时,我得到的响应是 HTTP 405 - Method not allowed.现在我知道我正在使用 POST 向 Get
方法发出请求,但很久以前我了解到它出于某种原因更安全,并且通常工作正常。
这里的要点是,这个经过验证的模式一直对我有效,并且对整个应用程序中的所有其他此类控制器和 POST 请求也有效。是什么导致 "/api/Branch"
的请求失败?
更新: 我将操作方法签名更改为如下所示,现在工作正常:
[HttpPost]
[Route("Get")]
public async Task<IEnumerable<Branch>> Fetch()
这很奇怪,因为 POST 请求直接作用于所有其他控制器上的 Get
操作,只要存在 HttpPost
属性。我的问题已经解决,但这个问题仍然悬而未决,为什么。与 Jinish 的回答相反,路由前缀开头的 /
似乎没有区别。有些控制器有,有些没有,但它们都能正常工作,除了 BranchController
.
我不相信你的 [RoutePrefix] 可以以 '/' 开头 应该是 [RoutePrefix("api/Branch")]
我使用的是 System.Web.Mvc.AllowAnonymousAttribute
而不是 System.Web.Http.AllowAnonymousAttribute
。使用现代便利设施,当我添加属性时,系统提示我(我认为是 ReSharper,但可能是 VS 本身)为它导入一个命名空间。在我匆忙中,我没有注意到它们是两个名称空间作为选项,我选择了错误的一个。更正如下:
using System.Collections.Generic;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web.Http;
//using System.Web.Mvc;