2017-05-16 7 views
2

は完璧に動作します:のHttpClient POST 405を返します(メソッド許可されていない)

POST http://localhost:51072/api/cs/processor/process 

{ 
'code':'TEST', 
    'mode':0 
} 

これは私のウェブAPIコントローラです私は深く掘った後、それは405を返しまったく同じ要求データでこのメソッドにアクセスしようとする方法

[RoutePrefix("api/cs")] 
public class ProcessorController : UmbracoApiController 
{... 

[AllowAnonymous] 
[Route("processor/process")] 
[HttpPost()] 
public IHttpActionResult Process([FromBody] ProcessSurvey dto) 
{ 
    if (!HttpContext.Current.Request.IsLocal) 
    { 
     return Content(HttpStatusCode.Forbidden, "Not allowed to start survey from remote host."); 
    } 
    ProcessSurveyResponse resp = new ProcessSurveyResponse(); 
    switch (dto.Code) 
    { 
     case "ITS": 
      resp = Execute(dto.Mode); 
      break; 
     default: 
      resp.Message = "Test successful!"; 
      break; 
    } 
    return Content(HttpStatusCode.Created, resp); 
} 

私は内部的にリダイレクト(302)を行うのことがわかってきましたが。しかし、何が起きているのか、なぜIISログに何があるのか​​についての情報は見つけられません。

私がこれまで行ってきたWebDAVを除去してどのような、属性があるのHttpないMVC、ルート設定のすべての星座(belkowが私の現在のものです)

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


    config.Routes.MapHttpRoute(
     name: "DefaultApiFull", 
     routeTemplate: "api/{controller}/{action}/{id}", 
     defaults: new { id = RouteParameter.Optional }); 

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

と、これは私のinitメソッドである

GlobalConfiguration.Configure(WebApiConfig.Register); //WebApi 2 Register method 

本当に何をすべきか分かりません...今は2日間苦労していますし、どこをもっと見てもわかりません。ここで

はHttpClientをが

using (var client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Clear(); 
       client.DefaultRequestHeaders.ExpectContinue = false; 
       client.DefaultRequestHeaders.Accept.Clear(); 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); 
       var json = JsonConvert.SerializeObject(dto); 
       StringContent content = new StringContent(json, Encoding.UTF8, "application/json"); 
       log.Debug("Call " + baseUrl); 
       HttpResponseMessage response = await client.PostAsync("http://localhost:51072/api/cs/surveyprocessor/process/", content); 
       string data = await response.Content.ReadAsStringAsync(); 
       if (response.IsSuccessStatusCode) 
       { 
        result = JsonConvert.DeserializeObject<ProcessSurveyResponse>(data); 
       } 
       else 
       { 
        result.Message = data; 
       } 
       return result; 
      } 

を呼び出し、ここで私のDTO

public class ProcessSurvey 
{ 
    [JsonProperty(PropertyName = "mode")] 
    public int Mode { get; set; } 
    [JsonProperty(PropertyName = "code")] 
    public string Code { get; set; } 
    [JsonProperty(PropertyName = "customers")] 
    public IList<int> Customers { get; set; } 
} 

私はHttpClientを介して投稿するとき、WEBAPIモジュールに到達していないと思われることは興味深い何です...

> iisexpress.exe Information: 0 : Request, Method=GET, 
> Url=http://localhost:51072/api/cs/surveyprocessor/process/?AspxAutoDetectCookieSupport=1, 
> Message='http://localhost:51072/api/cs/surveyprocessor/process/?AspxAutoDetectCookieSupport=1' 
> iisexpress.exe Information: 0 : Message='SurveyProcessor', 
> Operation=NamespaceHttpControllerSelector.SelectController 
> iisexpress.exe Information: 0 : 
> Message='SSI.CSS.Serviceportal.CS.Controller.SurveyProcessorController', 
> Operation=DefaultHttpControllerActivator.Create iisexpress.exe 
> Information: 0 : 
> Message='SSI.CSS.Serviceportal.CS.Controller.SurveyProcessorController', 
> Operation=HttpControllerDescriptor.CreateController iisexpress.exe 
> Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' 
> formatter', 
> Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance 
> iisexpress.exe Information: 0 : Message='Selected 
> formatter='JsonMediaTypeFormatter', content-type='application/json; 
> charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate 
> iisexpress.exe Warning: 0 : Message='UserMessage='The requested 
> resource does not support http method 'GET'.'', 
> Operation=ApiControllerActionSelector.SelectAction, Status=405 
> (MethodNotAllowed), Exception=System.Web.Http.HttpResponseException: 
> Processing of the HTTP request resulted in an exception. Please see 
> the HTTP response returned by the 'Response' property of this 
> exception for details. bei 
> System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext 
> controllerContext) bei 
> System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext 
> controllerContext) bei 
> System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0() bei 
> System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter 
> traceWriter, HttpRequestMessage request, String category, TraceLevel 
> level, String operatorName, String operationName, Action`1 beginTrace, 
> Action execute, Action`1 endTrace, Action`1 errorTrace) iisexpress.exe 
> Warning: 0 : Message='UserMessage='The requested resource does not 
> support http method 'GET'.'', 
> Operation=SurveyProcessorController.ExecuteAsync, Status=405 
> (MethodNotAllowed), Exception=System.Web.Http.HttpResponseException: 
> Processing of the HTTP request resulted in an exception. Please see 
> the HTTP response returned by the 'Response' property of this 
> exception for details. bei 
> System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext 
> controllerContext) bei 
> System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext 
> controllerContext) bei 
> System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0() bei 
> System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter 
> traceWriter, HttpRequestMessage request, String category, TraceLevel 
> level, String operatorName, String operationName, Action`1 beginTrace, 
> Action execute, Action`1 endTrace, Action`1 errorTrace) bei 
> System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.System.Web.Http.Controllers.IHttpActionSelector.SelectAction(HttpControllerContext 
> controllerContext) bei 
> System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext 
> controllerContext, CancellationToken cancellationToken) bei 
> System.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.MoveNext() 
> --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei 
> System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task 
> task) bei 
> System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) bei 
> System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext() 
> iisexpress.exe Information: 0 : Response, Status=405 
> (MethodNotAllowed), Method=GET, 
> Url=http://localhost:51072/api/cs/surveyprocessor/process/?AspxAutoDetectCookieSupport=1, 
> Message='Content-type='application/json; charset=utf-8', 
> content-length=unknown' iisexpress.exe Information: 0 : 
> Operation=JsonMediaTypeFormatter.WriteToStreamAsync iisexpress.exe 
> Information: 0 : Operation=SurveyProcessorController.Dispose 
> 'iisexpress.exe' (CLR v4.0.30319: 
> /LM/W3SVC/2/ROOT-1-131394088034503203): Loaded 
> 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET 
> Files\root\4650863f\f01d951b\App_Web_ping.aspx.5f2dec3.f9ui73ad.dll'. 
> The thread 0x2830 has exited with code 0 (0x0). The thread 0x3f10 has 
> exited with code 0 (0x0). The thread 0x2104 has exited with code 0 
> (0x0). 
+0

405が返されたとき、どのように「まったく同じリクエストデータでこのメソッドにアクセスしようとしますか」?その部分は明確ではありません。 [mcve]内のhttpクライアントを使用して問題を再現する方法を示します。 – Nkosi

+0

投稿を編集しました – nhaberl

+0

呼び出されたURLでは 'surveyprocessor'が、コントローラコードでは' processor'と表示されています。それはタイプミスですか?あなたは間違ったURLを呼んでいますか? – Nkosi

答えて

0

FormsAuthおよびSessionProviderモジュールのcookieless = "AutoDetect"をcookieless = "UseCookies"に変更すると、すべてが期待どおりに機能します。

関連する問題