ASP.NET WebApi : (405) 方法不被允许 [英] ASP.NET WebApi : (405) Method Not Allowed
问题描述
我有一个 Web Api 控制器.它的行为非常奇怪.当我使用 PostMan 时,我可以访问 Web Api 上的 POST 方法,但是当我使用 .net 中的 HttpWebRequest 时,它返回 (405) Method Not Allowed.我把 Web Api 代码放在这里:
I have a Web Api controller. It behaves very strange. When I use PostMan I can access the POST method on the Web Api but when I use HttpWebRequest from .net it returns (405) Method Not Allowed. I put the Web Api code here:
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:
Here is my 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 请求的方式:
This is how I send the POST request from another .NET project:
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 中添加了以下代码:
I already added the following code in my web.config:
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
很奇怪,因为我可以从 PostMan 成功发送 POST 请求.邮递员将此代码发送到 API.
It is strange because I can send POST request successfully from PostMan. The PostMan send this code to the API.
POST /api/Test/buy HTTP/1.1
Host: domain.com
Cache-Control: no-cache
Postman-Token: 9220a4e6-e707-5c5f-ea61-55171a5dd95f
Content-Type: application/x-www-form-urlencoded
InvoiceDate=28012016&Result=1
对于解决问题的任何建议,我将不胜感激.
I will appreciate any suggestion to solve the problem.
推荐答案
我找到了解决方案.
我检查了 Fiddler 的请求.当我向 API 发送 POST 请求时,它会使用这个新参数自动重定向到相同的地址 AspxAutoDetectCookieSupport=1
I checked the request by Fiddler.When I send a POST request to API it automatically redirect to the same address with this new parameter AspxAutoDetectCookieSupport=1
如何删除 AspxAutoDetectCookieSupport=1
最后,我将 web.config 中的 cookieless="AutoDetect"
更改为 cookieless="UseCookies"
,问题解决了.
Finally I changed the cookieless="AutoDetect"
in web.config to cookieless="UseCookies"
and the problem solved.
这篇关于ASP.NET WebApi : (405) 方法不被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!