在 ASP.NET Web 表单中使用 ASP.NET Web API 时出现 "Method Not Allowed" 错误
Getting "Method Not Allowed" error when using ASP.NET Web API inside ASP.NET Web Forms
我正在尝试使用此处给出的代码实施 HMAC 身份验证:http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/。
我将此代码集成到我的 ASP.NET 网络表单应用程序中。我创建了一个名为 "HMACAPI" 的文件夹,并在其中添加了控制器和过滤器。我还安装了所有必需的 Nuget 包。这就是我实现服务方法的方式:
[HMACAuthentication]
[RoutePrefix("api/forms")]
public class FormsController : ApiController
{
[Route("")]
public IHttpActionResult Get()
{
ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
var Name = ClaimsPrincipal.Current.Identity.Name;
return Ok("test");
}
[Route("")]
public IHttpActionResult Post(string order)
{
return Ok(order);
}
}
这是我为API配置的路由:
GlobalConfiguration.Configure(APIWebFormsProject.API.WebApiConfig.Register);
但是当我使用 client.PostAsJsonAsync()
时,它显示 Method Not Allowed
错误。我尝试了各种 SO 问题,但 none 他们的回答很有帮助。
我试过的:
删除了 WebDAV
模块。
向 post 方法添加了 [HttpPost]
属性。
我正在使用“http://localhost:56697/api/forms/" URL to access the API. But I also tried "http://localhost:56697/api/forms" and "http://localhost:56697/api/forms/test”。
更新
根据 Obsidian Phoenix 的建议,我可以 运行 它没有 [HMACAuthentication]
属性。但我想通过 HMAC 身份验证来实现这一点。那么,这可能是什么原因呢?
我猜你的问题是将 HTTP POST 发送到端点 (api/forms),这与 HMACAuth 属性无关,对吗?
如果是这种情况,则不要将订单作为字符串发送,它应该作为包含字符串 属性 的 POCO 对象发送,如下所示应该有效:
public class OrderModel
{
public string Order { get; set; }
}
您的方法缺少 [FromBody]
属性。
为了使用 client.PostAsJsonAsync(url, "test")
,您的方法签名应如下所示:
[Route("")]
public IHttpActionResult Post([FromBody] string order)
{
return Ok(order);
}
同样,传递一个 POCO 对象:
[Route("")]
public IHttpActionResult Post([FromBody] OrderModel order)
{
return Ok(order);
}
最后,我解决了这个问题。我没想到这会是一个解决方案。
我更改了端口并且使用了本地 IIS 服务器 而不是 Visual Studio 开发服务器。虽然我在问这个问题之前使用的是 IIS,但我认为更改端口可以解决问题。
好的,谢谢大家为解决我的问题所做的努力。由于您的所有回答和评论,我才能够找到这个解决方案。 :)
我正在尝试使用此处给出的代码实施 HMAC 身份验证:http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/。
我将此代码集成到我的 ASP.NET 网络表单应用程序中。我创建了一个名为 "HMACAPI" 的文件夹,并在其中添加了控制器和过滤器。我还安装了所有必需的 Nuget 包。这就是我实现服务方法的方式:
[HMACAuthentication]
[RoutePrefix("api/forms")]
public class FormsController : ApiController
{
[Route("")]
public IHttpActionResult Get()
{
ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
var Name = ClaimsPrincipal.Current.Identity.Name;
return Ok("test");
}
[Route("")]
public IHttpActionResult Post(string order)
{
return Ok(order);
}
}
这是我为API配置的路由:
GlobalConfiguration.Configure(APIWebFormsProject.API.WebApiConfig.Register);
但是当我使用 client.PostAsJsonAsync()
时,它显示 Method Not Allowed
错误。我尝试了各种 SO 问题,但 none 他们的回答很有帮助。
我试过的:
删除了
WebDAV
模块。向 post 方法添加了
[HttpPost]
属性。
我正在使用“http://localhost:56697/api/forms/" URL to access the API. But I also tried "http://localhost:56697/api/forms" and "http://localhost:56697/api/forms/test”。
更新
根据 Obsidian Phoenix 的建议,我可以 运行 它没有 [HMACAuthentication]
属性。但我想通过 HMAC 身份验证来实现这一点。那么,这可能是什么原因呢?
我猜你的问题是将 HTTP POST 发送到端点 (api/forms),这与 HMACAuth 属性无关,对吗?
如果是这种情况,则不要将订单作为字符串发送,它应该作为包含字符串 属性 的 POCO 对象发送,如下所示应该有效:
public class OrderModel
{
public string Order { get; set; }
}
您的方法缺少 [FromBody]
属性。
为了使用 client.PostAsJsonAsync(url, "test")
,您的方法签名应如下所示:
[Route("")]
public IHttpActionResult Post([FromBody] string order)
{
return Ok(order);
}
同样,传递一个 POCO 对象:
[Route("")]
public IHttpActionResult Post([FromBody] OrderModel order)
{
return Ok(order);
}
最后,我解决了这个问题。我没想到这会是一个解决方案。
我更改了端口并且使用了本地 IIS 服务器 而不是 Visual Studio 开发服务器。虽然我在问这个问题之前使用的是 IIS,但我认为更改端口可以解决问题。
好的,谢谢大家为解决我的问题所做的努力。由于您的所有回答和评论,我才能够找到这个解决方案。 :)