25

में काम नहीं कर रहा है, मुझे लगता है कि एएसपी.Net वेब एपीआई में बाध्यकारी मॉडल एमवीसी द्वारा समर्थित कार्यक्षमता के समान न्यूनतम स्तर के साथ बाध्यकारी का समर्थन करना था।एएसपी.Net वेब एपीआई मॉडल बाध्यकारी एमवीसी 3

public class WordsController : ApiController 
{ 
    private string[] _words = new [] { "apple", "ball", "cat", "dog" }; 

    public IEnumerable<string> Get(SearchModel searchSearchModel) 
    { 
     return _words 
      .Where(w => w.Contains(searchSearchModel.Search)) 
      .Take(searchSearchModel.Max); 
    } 
} 

public class SearchModel 
{ 
    public string Search { get; set; } 
    public int Max { get; set; } 
} 

मैं के साथ यह अनुरोध कर रहा हूँ:

निम्नलिखित नियंत्रक लो

http://localhost:62855/api/words?search=a&max=2 

दुर्भाग्य के रूप में यह MVC में होगा मॉडल बाध्य नहीं है। यह बाध्यकारी क्यों नहीं है जैसा कि मैं उम्मीद करता हूं? मेरे आवेदन में मेरे पास कई अलग-अलग मॉडल प्रकार होंगे। यह अच्छा होगा अगर बाध्यकारी सिर्फ काम करता है, जैसे यह एमवीसी में करता है।

+0

हो सकता है, आपकी मदद कर इस [पोस्ट] [1] समस्या। [1]: http://stackoverflow.com/questions/12072277/reading-fromuri-and-frombody-at-the-same-time – Cagdas

उत्तर

27

इस पर एक नज़र डालें: How WebAPI does Parameter Binding

तुम इतनी तरह अपने जटिल पैरामीटर को सजाने के लिए की जरूरत है:

public IEnumerable<string> Get([FromUri] SearchModel searchSearchModel) 

या

public IEnumerable<string> Get([ModelBinder] SearchModel searchSearchModel) 
1

मैं पूरी वेब एपीआई 2 पाया है होना करने के लिए बहुत सारे "गॉटचास" के साथ एक कठिन सीखने की वक्र मैंने कुछ महत्वपूर्ण किताबें पढ़ी हैं जो इस समृद्ध उत्पाद की पेशकश के कई आर्केन बारीकियों को कवर करती हैं। लेकिन मूल रूप से, मैंने सोचा कि कुछ मूल कार्यक्षमता होनी चाहिए जो कि सर्वोत्तम सुविधाओं का लाभ उठा सकती है। तो, मैंने चार सीधे आगे के कार्यों को करने के लिए तैयार किया। 1. एक ब्राउज़र से, एक एपीआई 2 क्लाइंट में एक क्वेरी स्ट्रिंग स्वीकार करें और एक साधारण .NET मॉडल पॉप्युलेट करें। 2. ग्राहक को async पूर्व मॉडल से निकाले गए JSON में एन्कोड किए गए एपीआई 2 सर्वर पर पोस्ट करें 3. क्या ग्राहक क्लाइंट से पोस्ट अनुरोध पर एक छोटा रूपांतरण करता है। 4. इसे ब्राउज़र पर वापस बैक करें। यह बात है।

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

 
namespace Combined.Controllers // This is an ASP.NET Web Api 2 Story 
 
{ 
 
    // Paste the following string in your browser -- the goal is to convert the last name to lower case 
 
    // The return the result to the browser--You cant click on this one. This is all Model based. No Primitives. 
 
    // It is on the Local IIS--not IIS Express. This can be set in Project->Properties=>Web http://localhost/Combined with a "Create Virtual Directory" 
 
    // http://localhost/Combined/api/Combined?FirstName=JIM&LastName=LENNANE // Paste this in your browser After the Default Page it displayed 
 
    // 
 
    public class CombinedController : ApiController 
 
    { 
 
     // GET: api/Combined This handels a simple Query String request from a Browser 
 
     // What is important here is that populating the model is from the URI values NOT the body which is hidden 
 
     public Task<HttpResponseMessage> Get([FromUri]FromBrowserModel fromBrowser) 
 
     { 
 
      // 
 
      // The Client looks at the query string pairs from the Browser 
 
      // Then gets them ready to send to the server 
 
      // 
 
      RequestToServerModel requestToServerModel = new RequestToServerModel(); 
 
      requestToServerModel.FirstName = fromBrowser.FirstName; 
 
      requestToServerModel.LastName = fromBrowser.LastName; 
 
      // Now the Client send the Request to the Server async and everyone awaits the Response 
 
      Task<HttpResponseMessage> response = PostAsyncToApi2Server("http://localhost/Combined/api/Combined", requestToServerModel); 
 
      return response; // The response from the Server should be sent back to the Browser from here. 
 
     } 
 
     async Task<HttpResponseMessage> PostAsyncToApi2Server(string uri, RequestToServerModel requestToServerModel) 
 
     { 
 
      using (var client = new HttpClient()) 
 
      { 
 
       // Here the Method waits for the Request to the Server to complete 
 
       return await client.PostAsJsonAsync(uri, requestToServerModel) 
 
        .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()); 
 
      } 
 
     } 
 
     // POST: api/Combined This Handles the Inbound Post Request from the Client 
 
     // NOTICE THE [FromBody] Annotation. This is the key to extraction the model from the Body of the Post Request-- not the Uri ae in [FromUri] 
 
     // Also notice that there are no Async methods here. Not required, async would probably work also. 
 
     // 
 
     public HttpResponseMessage Post([FromBody]RequestToServerModel fromClient) 
 
     { 
 
      // 
 
      // Respond to an HttpClient request Synchronously 
 
      // The model is serialised into Json by specifying the Formatter Configuration.Formatters.JsonFormatter 
 
      // Prep the outbound response 
 
      ResponseToClientModel responseToClient = new ResponseToClientModel(); 
 
      // 
 
      // The conversion to lower case is done here using the Request Body Data Model 
 
      //    
 
      responseToClient.FirstName = fromClient.FirstName.ToLower(); 
 
      responseToClient.LastName = fromClient.LastName.ToLower(); 
 
      // 
 
      // The Client should be waiting patiently for this result 
 
      // 
 
      using (HttpResponseMessage response = new HttpResponseMessage()) 
 
      { 
 
       return this.Request.CreateResponse(HttpStatusCode.Created, responseToClient, Configuration.Formatters.JsonFormatter); // Respond only with the Status and the Model 
 
      } 
 
     } 
 
     public class FromBrowserModel 
 
     { 
 
      public string FirstName { get; set; } 
 
      public string LastName { get; set; } 
 
     } 
 
     public class RequestToServerModel 
 
     { 
 
      public string FirstName { get; set; } 
 
      public string LastName { get; set; } 
 
     } 
 

 
     public class ResponseToClientModel 
 
     { 
 
      public string FirstName { get; set; } 
 
      public string LastName { get; set; } 
 
     } 
 

 
     
 
    } 
 
}

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