我有一个 Web Api Controller ,我已经在本地主机上测试了所有操作,它运行良好。但是当我在 Web 服务器上发布它时,只需使用 [HttpGet]
进行操作并且 [ HttpPost]
操作返回 Http 405 错误
public class ContentController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> AdvantageList()
{
//return ok 200
}
[HttpPost]
public async Task<IHttpActionResult> SaveAdvantage(ContentModel model)
{
//return 405
}
}
我在客户端使用了下面的方法
var r = await ClientManager.Client.PostAsJsonAsync("api/Content/SaveAdvantage", Advantage);
但它会在响应表单服务器下重新运行。我使用了 PostAsJsonAsync
方法,但它说 请求的资源不支持 http 方法 'GET'
有人知道为什么吗?
{ StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.0, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache X-Powered-By-Plesk: PleskWin Connection: close Cache-Control: no-cache Date: Fri, 29 Sep 2017 08:53:51 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 72 Allow: POST Content-Type: application/json; charset=utf-8 Expires: -1 }}
和
"{\"message\":\"The requested resource does not support http method 'GET'.\"}"
我的 web api 配置中有以下内容:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}");
config.Routes.MapHttpRoute("WithId", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute("TwoId", "api/{controller}/{action}/{id}/{id2}", new { id = RouteParameter.Optional, id2 = RouteParameter.Optional });
config.MapHttpAttributeRoutes();
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}
}
我在 Web.config 中有以下处理程序
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我在我的 winform 应用程序中使用这个 webapi2 应用程序。
最佳答案
是http错误码告诉我们这是一个正确的问题。看看:https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP
关于c# - 发布 Web API 2 应用程序后出现 HTTP 405 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46485219/