ASP.NET WebApi:(405)方法不允许
我有一个Web Api控制器。它的行为很奇怪。当我使用PostMan时,我可以访问Web Api上的POST方法,但当我使用.net中的HttpWebRequest时,它返回(405)不允许的方法。 我将Web Api代码放在这里:ASP.NET WebApi:(405)方法不允许,asp.net,asp.net-mvc,asp.net-web-api,Asp.net,Asp.net Mvc,Asp.net Web Api,我有一个Web Api控制器。它的行为很奇怪。当我使用PostMan时,我可以访问Web Api上的POST方法,但当我使用.net中的HttpWebRequest时,它返回(405)不允许的方法。 我将Web Api代码放在这里: using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; us
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace MyProject.Controllers
{
public class TestController : ApiController
{
[HttpPost]
public int buy(OrderResult value)
{
try
{
if (value.InvoiceDate != null && value.Result == 1)
{
return 0;
}
}
catch{}
return -1;
}
}
public class OrderResult
{
public int Result { get; set; }
public long InvoiceNumber { get; set; }
public string InvoiceDate { get; set; }
public long TimeStamp { get; set; }
}
}
这是我的WebApiConfig.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MyProject
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);
}
}
}
以下是我从另一个.NET项目发送POST请求的方式:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace MyProject.Controllers
{
public static class WebReq
{
public static string PostRequest(string Url, string postParameters)
{
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url);
myReq.Method = "POST";
myReq.Timeout = 30000;
myReq.Proxy = null;
byte[] postData = Encoding.UTF8.GetBytes(postParameters);
myReq.ContentLength = postData.Length;
myReq.ContentType = "application/x-www-form-urlencoded";
using (Stream requestWrite = myReq.GetRequestStream())
{
requestWrite.Write(postData, 0, postData.Length);
requestWrite.Close();
using (HttpWebResponse webResponse = (HttpWebResponse)myReq.GetResponse())
{
if (webResponse.StatusCode == HttpStatusCode.OK)
{
using (Stream str = webResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
return sr.ReadToEnd();
}
}
}
return null;
}
}
}
catch (Exception e)
{
var message = e.Message;
return null;
}
}
}
}
我已经在我的web.config中添加了以下代码:
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
如果有任何解决这个问题的建议,我将不胜感激
byte[] postData = Encoding.UTF8.GetBytes(postParameters);
myReq.ContentLength = postData.Length;
myReq.ContentType = "application/x-www-form-urlencoded";
using (Stream requestWrite = myReq.GetRequestStream())
{
requestWrite.Write(postData, 0, postData.Length);
您很可能没有发送正确的x-www-form-urlencoded请求。您可能没有正确地编码作为后参数传入的数据。看
由于您没有根据x-www-form-urlencoded生成有效的OrderResult对象,因此路由选择器不会选择您的Buy
操作。这就是为什么你得到的职位是不允许的
如果更改了OrderResult value=null
,则可能会访问控制器,因为它现在是一个可选参数。然而,这并不是您想要的,除非您将拥有一个行为类似于:
Buy(OrderResult value = null)
{
if(value== null)
value = MyCustomDeserializeFromRequestBody(RequestContext)
...
}
最终,你真的不应该使用这个类。我脑子里有更好的现代开发结构。用fiddler或类似的东西捕捉你的请求,你可能正在发送“选项”方法 这与CORS有关,所以我认为您的解决方案是在WebAPI上启用CORS 我找到了解决办法 我通过Fiddler检查了请求。当我向API发送POST请求时,它会自动重定向到具有此新参数的同一地址
AspxAutoDetectCookieSupport=1
最后,我将web.config中的
cookieless=“AutoDetect”
更改为cookieless=“UseCookies”
,问题就解决了。我知道这是一篇老文章,但这可能会帮助其他人。我刚刚遇到了一个类似的问题,当我在《邮递员》上发表文章时,收到了一个405错误“Get not allowed”。事实证明,我是使用http而不是https提交到URL的。更改为https修复了它。在我的例子中,我在项目中有一个与路由同名的物理文件夹(例如沙盒),任何POST请求都被IIS中的静态文件处理程序(显然)拦截,而不是WebAPI运行时
得到一个误导性的405错误,而不是更预期的404,是我花了一些时间进行故障排除的原因
这不容易,但也是可能的。希望它能帮助别人。这不是CORS的问题,这实际上是我的第一个想法,但OP完全是从服务器/应用程序运行的,而不是在浏览器+ajax中。简单而简洁。谢谢,保罗。谢谢,这件事发生在我身上,完全不明显,我不知道没有这个建议我会怎么发现。
Buy(OrderResult value = null)
{
if(value== null)
value = MyCustomDeserializeFromRequestBody(RequestContext)
...
}