2

我有一个 Web Api 控制器,我已经在 localhost 上测试了所有操作,它运行良好。但是当我在 Web 服务器上发布它时,Just Actions with [HttpGet]works and [HttpPost]Actions return Http 405 error

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了方法,但它说The requested resource does not support http method '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 :无缓存 日期:2017 年 9 月 29 日星期五 08:53:51 GMT 服务器:Microsoft-IIS/8.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET Content-Length:72 允许:POST内容类型:应用程序/json;charset=utf-8 过期:-1 }}

"{\"message\":\"请求的资源不支持http方法'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 应用程序。

4

3 回答 3

0

在我的网络服务器中托管设置时,有一个属性首选域,我将其更改为,之后它可以正常工作

首选域设置

于 2017-09-30T11:02:58.573 回答
0

是 http 错误代码告诉我们这是一个正确的问题。看看:https ://fr.wikipedia.org/wiki/Liste_des_codes_HTTP

于 2017-09-29T13:44:00.063 回答
-1

您在 Web 服务器上存在权限问题。405 http 代码表示不允许从您的 Web 服务器使用 post 方法。确保您在发送请求时已通过身份验证,并确保您的请求包含一些用于身份验证的令牌。

于 2017-09-29T09:08:00.753 回答