WebApi + AngularJS: 405-Methode Nicht Erlaubt

Ich habe da ein problem und zwar ich habe gesehen, viele Antworten zu dem gleichen Thema, nichts hat wirklich funktioniert.. ich habe eine WebAPI-Projekt und ein MVC ein, wo ich Eckige.

Aus der Winkel-Projekt, ich bin versucht, etwas zu veröffentlichen wie diesen:

var promise = $http.post("http://localhost:55692/api/users", user)
                .success(function (data) {
                    debugger;
                    console.log(data);
                    return data;
                });

            $http(
        {
            url: "http://localhost:55692/api/users",
            method: "POST",
            headers: { 'Content-Type': 'application/json' },
            data: { "user": user }
        })
            .then(function (response) {
                debugger;
                console.log(data);
                return data;
            });
            return promise;

Wenn Sie sehen, rufe ich die gleiche API zweimal in Fall ist es eine Eckige Sache, aber für mich ist das nicht..

Habe ich diesen controller in meinem API-Projekt:

[RoutePrefix("api/users")]
    public class ValuesController : ApiController
    {
        [Route("")]
        [HttpPost]
        public HttpResponseMessage LoginOrRegister(RegisterUserViewModel user)
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, true);
            return response;
        }
    }

Dies ist die Antwort, die ich habe:

Antwort

- Und das ist meine WebConfig

<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>
    <!--<validation validateIntegratedModeConfiguration="false" />-->
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, X-Token, X-Acting-As" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS,PATCH" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

BEARBEITEN
Meine WebApiConfig sieht wie folgt aus:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //Attribute routing enabled
            config.MapHttpAttributeRoutes();

            //Convention-based routing enabled
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include;

            //Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            //To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            //For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();

            //To disable tracing in your application, please comment out or remove the following line of code
            //For more information, refer to: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();
        }
    }

Schließlich, das einzige was "komisch" ist, dass ich mit IISExpress als diese sind die ersten Schritte für mein Projekt..

Weiß jemand, was könnte passiert sein?

  • Was macht Ihr Register Methode in WebApiConfig.cs Aussehen? Auch, nur als eine Allgemeine Beratung, ich möchte vermeiden, erstellen Routen Präfixe, die nicht mit dem controller-Namen. Wenn Ihr Projekt wächst, wird es zunehmend frustrierend, navigieren Sie Ihren code aus, wenn der controller-name nicht mit dem route-Präfix. Noch mehr so, wenn Sie in einem team arbeiten.
  • Gibt es, ich habe es.. Sie haben einen Punkt gibt, aber ehrlich, es ist nur ein test-controller, um die cross-domain-Sache arbeiten..
InformationsquelleAutor Ger Stark | 2016-07-28
Schreibe einen Kommentar