Ich habe einen Web-API-Controller und ich habe alle Aktionen auf localhost getestet und es funktioniert gut.Aber wenn ich es auf Web Server veröffentlicht, funktioniert nur Aktionen mit [HttpGet]
funktioniert und [HttpPost]
Aktionen geben Http zurück 405 FehlerHTTP 405 Fehler nach der Veröffentlichung Web API 2 Anwendungen
public class ContentController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> AdvantageList()
{
//return ok 200
}
[HttpPost]
public async Task<IHttpActionResult> SaveAdvantage(ContentModel model)
{
//return 405
}
}
I unten Methode auf dem Client verwendet
var r = await ClientManager.Client.PostAsJsonAsync("api/Content/SaveAdvantage", Advantage);
aber es wird unter Antwortformular server.I Gebraucht PostAsJsonAsync
Methode retrun aber es sagt, dass The requested resource does not support http method 'GET'
Weiß jemand warum?
{ Statuscode: 405, ReasonPhrase: 'Methode nicht erlaubt', Version: 1.0, Inhalt: System.Net.Http.StreamContent, Sockel: { Pragma: no-cache X-Powered-By -Plesk: PleskWin Anschluss: schließen Cache-Control: no-cache Datum: Fr, 29. September 2017 08.53.51 GMT Server: Microsoft-IIS/8.0 X-ASPNET-Version: 4.0.30319 X -Powered-By: ASP.NET Content-Length: 72 Erlauben: POST Content-Type: application/json; charset = utf-8 Gültig bis: -1 }}
Und
"{\" Meldung \ ": \". Die angeforderte Ressource http-Methode 'GET' nicht unterstützt \“ „}
ich habe die unten in meiner Web-api config:
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();
}
}
ich habe unten Handler in 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>
Ich benutze diese Webapi2-Anwendung in meiner Winform-Anwendung.
Nein, das contorller ist erlaubt für anonymouse, wenn dieser Grund ein Problem verursacht, warum ich alle Vorteilsinhalte erfolgreich bekomme? – mohsen