ASP.NET WebApi:(405)方法不允许
|
我有一个Web Api控制器.它表现得很奇怪.当我使用PostMan时,我可以在Web Api上访问POST方法,但是当我从.net使用HttpWebRequest时,它返回(405)Method Not Allowed.
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,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> 这很奇怪,因为我可以从PostMan成功发送POST请求. PostMan将此代码发送到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 我将不胜感激任何解决问题的建议. 解决方法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,postData.Length);
您很可能不会发送正确的x-www-form-urlencoded请求.您可能无法正确编码从postParameters传入的数据.见Post form data using HttpWebRequest 由于您未根据x-www-form-urlencoded生成有效的OrderResult对象,因此路由选择器不会选择您的Buy操作.这就是你不允许POST的原因. 如果您更改了OrderResult value = null,则可能会进入控制器,因为它现在是可选参数.然而,这不是你想要的,除非你有一个奇怪的控制器,行为如下: Buy(OrderResult value = null)
{
if(value== null)
value = MyCustomDeserializeFromRequestBody(RequestContext)
...
}
最后你真的不应该使用这个类,现代开发有更好的结构https://stackoverflow.com/a/31147762/37055 https://github.com/hhariri/EasyHttp我的头顶 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – Azure服务架构与Docker一样吗?
- asp.net 4路由不工作在iis 7
- asp.net – 如何尊重“从无Cookie域中提供静态内容”IIS6中
- asp.net-mvc – 使用asp.net属性路由的根路径的默认路由
- 实体框架 – 使用EF和WebAPI,如何返回一个ViewModel并支持I
- .net – VS2017 15.3解决方案文件中的新GlobalSection是什么
- 如何在asp.net mvc 2中获取Html.EditorForModel()方法的tex
- asp.net-mvc-3 – Html.RenderPartial和Ajax.BeginForm –
- asp.net-mvc – ASP.NET MVC的初学者在线资源是什么?
- .net – MVC3部分视图与常规视图
- asp.net-mvc – 如何在ASP.NET MVC RC1中返回304
- asp.net – MaintainScrollPositionOnPostback属
- asp.net – 如何将转发器中Item的客户端ID传递给
- iis – ServerManager构造函数在测试环境中崩溃
- asp.net – ViewState作为属性
- 将链接列添加到ASP.NET GridView
- asp.net-mvc – 添加MVC控件或视图时,Visual Stu
- asp.net – “无法启动IIS Express Web服务器”错
- asp.net – 如何获得大型LinkedIn图像共享格式
- asp.net-mvc – 使用ASP.NET MVC测试驱动的开发
