2016-01-28 13 views
5

मेरे पास एक वेब एपीआई नियंत्रक है। यह बहुत अजीब व्यवहार करता है। जब मैं PostMan का उपयोग करता हूं तो मैं वेब एपीआई पर POST विधि तक पहुंच सकता हूं लेकिन जब मैं .NET से HttpWebRequest का उपयोग करता हूं तो यह लौटाता है (405) विधि अनुमत नहीं है। मैं यहाँ वेब एपीआई कोड डाल:एएसपी.नेट वेबएपीआई: (405) विधि अनुमत नहीं

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 

namespace MyProject 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 

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

     } 
    } 
} 

यह कैसे मैं एक और नेट परियोजना से पोस्ट अनुरोध भेज है:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Web; 

namespace MyProject.Controllers 
{ 
    public static class WebReq 
    { 
     public static string PostRequest(string Url, string postParameters) 
     { 
      try 
      { 
       HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url); 
       myReq.Method = "POST"; 
       myReq.Timeout = 30000; 
       myReq.Proxy = null; 

       byte[] postData = Encoding.UTF8.GetBytes(postParameters); 
       myReq.ContentLength = postData.Length; 
       myReq.ContentType = "application/x-www-form-urlencoded"; 
       using (Stream requestWrite = myReq.GetRequestStream()) 
       { 
        requestWrite.Write(postData, 0, postData.Length); 
        requestWrite.Close(); 
        using (HttpWebResponse webResponse = (HttpWebResponse)myReq.GetResponse()) 
        { 
         if (webResponse.StatusCode == HttpStatusCode.OK) 
         { 
          using (Stream str = webResponse.GetResponseStream()) 
          { 
           using (StreamReader sr = new StreamReader(str)) 
           { 
            return sr.ReadToEnd(); 
           } 
          } 
         } 
         return null; 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       var message = e.Message; 
       return null; 
      } 
     } 

    } 
} 

using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web; 
using System.Web.Http; 

namespace MyProject.Controllers 
{ 
    public class TestController : ApiController 
    { 
     [HttpPost] 
     public int buy(OrderResult value) 
     { 
      try 
      { 
       if (value.InvoiceDate != null && value.Result == 1) 
       { 
        return 0; 
       } 
      } 
      catch{} 

      return -1; 
     } 
    } 

    public class OrderResult 
    { 
     public int Result { get; set; } 
     public long InvoiceNumber { get; set; } 
     public string InvoiceDate { get; set; } 
     public long TimeStamp { get; set; } 
    } 
} 

यहाँ मेरी WebApiConfig.cs है

मैंने पहले से ही अपने वेब.config में निम्न कोड जोड़ा है:

<modules runAllManagedModulesForAllRequests="true"> 
    <remove name="WebDAVModule" /> 
</modules> 

यह अजीब बात है क्योंकि मैं PostMan से सफलतापूर्वक POST अनुरोध भेज सकता हूं। PostMan एपीआई को यह कोड भेजो।

POST /api/Test/buy HTTP/1.1 
Host: domain.com 
Cache-Control: no-cache 
Postman-Token: 9220a4e6-e707-5c5f-ea61-55171a5dd95f 
Content-Type: application/x-www-form-urlencoded 

InvoiceDate=28012016&Result=1 

मैं समस्या को हल करने के लिए किसी भी सुझाव की सराहना करता हूं।

उत्तर

1

मैं समाधान सक्षम करने के लिए Cors हो जाएगा।

मैं Fiddler.When द्वारा अनुरोध जाँच मैं यह स्वचालित रूप से एपीआई इस नए पैरामीटर AspxAutoDetectCookieSupport=1

How to remove AspxAutoDetectCookieSupport=1

अंत में मैं करने के लिए web.config में cookieless="AutoDetect" बदल के साथ एक ही पते पर रीडायरेक्ट करने के लिए एक पोस्ट अनुरोध भेजने cookieless="UseCookies" और समस्या हल हो गई।

1
 byte[] postData = Encoding.UTF8.GetBytes(postParameters); 
      myReq.ContentLength = postData.Length; 
      myReq.ContentType = "application/x-www-form-urlencoded"; 
      using (Stream requestWrite = myReq.GetRequestStream()) 
      { 
       requestWrite.Write(postData, 0, postData.Length); 

आप संभवतः एक उचित एक्स-www-form-urlencoded अनुरोध नहीं भेज रहे हैं। आप पोस्ट पैरामीटर के रूप में जो कुछ भी पारित हो चुके हैं, उससे डेटा को ठीक से एन्कोड नहीं कर रहे हैं। Post form data using HttpWebRequest

चूंकि आप x-www-form-urlencoded के अनुसार एक मान्य ऑर्डर रेसल्ट ऑब्जेक्ट नहीं बना रहे हैं, तो रूट चयनकर्ता आपकी Buy क्रिया का चयन नहीं करता है। यही कारण है कि आप POST प्राप्त करने की अनुमति नहीं है।

यदि आप OrderResult value = null बदल चुके हैं तो आप नियंत्रक के पास पहुंचेंगे क्योंकि यह अब एक वैकल्पिक पैरामीटर है। हालांकि यह नहीं है कि आप क्या चाहते, जब तक आप एक अजीब नियंत्रक व्यवहार करती है कि ऐसा करने जा रहे हैं:

Buy(OrderResult value = null) 
{ 
    if(value== null) 
     value = MyCustomDeserializeFromRequestBody(RequestContext) 

    ... 
} 

अंततः तुम सिर्फ सच में इस वर्ग के आधुनिक विकास https://stackoverflow.com/a/31147762/37055https://github.com/hhariri/EasyHttp के लिए काफी बेहतर निर्माणों देखते हैं उपयोग नहीं करना चाहिए मेरे सिर के शीर्ष से

-1

फिडलर या कुछ समान के साथ अपना अनुरोध पकड़ें, आप शायद "विकल्प" विधि भेज रहे हैं।

कि CORS से संबंधित है, इसलिए मुझे लगता है आप के लिए समाधान वेबएपीआई

पर

http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

+1

यह एक कॉरस मुद्दा नहीं है, जो वास्तव में मेरा पहला विचार था लेकिन ओपी पूरी तरह से एक सर्वर/एप्लिकेशन से चल रहा है जो ब्राउज़र + AJAX में नहीं है। –

+0

आआ हाँ आप सही हैं .. क्षमा करें: डी – hellwd

संबंधित मुद्दे