2015-08-14 7 views
9

में एक कस्टम प्रतिक्रिया शीर्ष लेख जोड़ें, मैं एक GET विधि है कि निम्नलिखित की तरह देखा था:अब तक ApiController

protected override async Task<IHttpActionResult> GetAll(QueryData query) 
{ 
    // ... Some operations 

    //LINQ Expression based on the query parameters 
    Expression<Func<Entity, bool>> queryExpression = BuildQueryExpression(query); 

    //Begin to count all the entities in the repository 
    Task<int> countingEntities = repo.CountAsync(queryExpression); 

    //Reads an entity that will be the page start 
    Entity start = await repo.ReadAsync(query.Start); 

    //Reads all the entities starting from the start entity 
    IEnumerable<Entity> found = await repo.BrowseAllAsync(start, queryExpression); 

    //Truncates to page size 
    found = found.Take(query.Size); 

    //Number of entities returned in response 
    int count = found.Count(); 

    //Number of total entities (without pagination) 
    int total = await countingEntities; 

    return Ok(new { 
      Total = total, 
      Count = count, 
      Last = count > 0 ? GetEntityKey(found.Last()) : default(Key), 
      Data = found.Select(e => IsResourceOwner(e) ? MapToOwnerDTO(e) : MapToDTO(e)).ToList() 
    }); 
} 

यह एक आकर्षण की तरह काम किया है और यह अच्छा है। हालांकि, मुझे हाल ही में प्रतिक्रिया मेटाडाटा (यानी Total, Count और Last गुण) प्रतिक्रिया प्रतिक्रिया के बजाय प्रतिक्रिया कस्टम हेडर भेजने के लिए कहा गया था।

मैं एपीकंट्रोलर से Response तक पहुंचने का प्रबंधन नहीं कर सकता। मैंने फ़िल्टर या विशेषता के बारे में सोचा, लेकिन मुझे मेटाडेटा मान कैसे मिलेगा?

मैं इस जानकारी को प्रतिक्रिया पर रख सकता हूं और उसके बाद एक फ़िल्टर है जो क्लाइंट को भेजे जाने से पहले प्रतिक्रिया को विलुप्त कर देगा, और शीर्षकों के साथ एक नया निर्माण करेगा, लेकिन यह परेशानी और खराब लगता है।

क्या ApiController पर सीधे इस विधि से कस्टम शीर्षलेख जोड़ने का कोई तरीका है? ,

public class AddCustomHeaderFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) 
    { 
     actionExecutedContext.Response.Content.Headers.Add("name", "value"); 
    } 
} 
+1

जितना सरल होना चाहिए [उस] (http://stackoverflow.com/questions/13487012/mvc-4-web-api-add-custom-response-http-header) – Andrei

+0

@Andrei मेरे पास नहीं है एक 'HttpContext' प्रॉपर्टी, लेकिन मेरे पास' एक्शनकॉन्टेक्स्ट 'है। हालांकि, उस ऑब्जेक्ट की 'प्रतिक्रिया' संपत्ति 'शून्य' है और मैं इसके साथ काम नहीं कर सकता। –

+0

आपको वास्तव में प्रतिक्रिया बनाने के लिए ActionContext.Request.CreateResponse() का उपयोग करने की आवश्यकता है और फिर – harishr

उत्तर

10

मैंने दर्ज किया है टिप्पणी यहाँ मेरा पूरा जवाब है:

+2

यह पूरी तरह से काम करता है लेकिन क्या यह करने का सही तरीका है? मेरा मेटाडेटा प्रतिक्रिया की संपत्ति होनी चाहिए, अनुरोध नहीं। मेरा मतलब है, यह एक समाधान के रूप में काम करता है, लेकिन क्या यह अवधारणात्मक रूप से सही है? –

+2

यह मुझे दोहरे काम की तरह दिखता है। आप एक हेडर [सीधे] (https://stackoverflow.com/a/45489505/2156743) – Nikola

+0

@ निकोला जोड़ सकते हैं लेकिन फिर आप दृढ़ता से टाइप की गई प्रतिक्रिया खो देते हैं, जिसे ओपी का उपयोग नहीं किया गया था लेकिन अभी भी इस दृष्टिकोण के साथ एक विकल्प है । मैं एक वेब एपीआई प्रोजेक्ट पर काम कर रहा हूं और मजबूत प्रकारों का उपयोग नहीं कर रहा हूं - एक के लिए हम आसानी से सही swagger उत्पन्न नहीं कर सकते हैं। अगर आप –

-2

आपको लगता है कि आप कस्टम हेडर भेज सकते हैं और HttpContext का उपयोग करने की अनुमति देगा एक कस्टम ActionFilter उपयोग कर सकते हैं।

आपको एक कस्टम फ़िल्टर बनाने और अपने नियंत्रक को लागू करने की आवश्यकता होगी।

public class CustomHeaderFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) 
    { 
     var count = actionExecutedContext.Request.Properties["Count"]; 
     actionExecutedContext.Response.Content.Headers.Add("totalHeader", count); 
    } 
} 

अपने नियंत्रक

public class AddressController : ApiController 
     { 
      public async Task<Address> Get() 
      { 
       Request.Properties["Count"] = "123"; 
      } 
    } 
+0

लेकिन मुझे हेडर के" मान "कैसे प्राप्त होंगे? –

+0

दिलचस्प प्रश्न। ऐसा लगता है कि आप संपत्ति अनुरोध। प्रॉपर्टीज ["गणना"] = "123" नियंत्रक में सेट कर सकते हैं और फ़िल्टर में इसका उपयोग कर सकते हैं। – Yousuf

+0

फ़िल्टर में, आप इसे क्रिया से प्राप्त कर सकते हैं Context.Request.Properties ["गणना"] – Yousuf

8

आप स्पष्ट रूप से ऐसा तरह एक विधि में कस्टम शीर्षलेख जोड़ सकते में:

[HttpGet] 
[Route("home/students")] 
public HttpResponseMessage GetStudents() 
{ 
     // Get students from Database 

     // Create the response 
     var response = Request.CreateResponse(HttpStatusCode.OK, studends); 

     // Set headers for paging 
     response.Headers.Add("X-Students-Total-Count", studends.Count()); 

     return response; 
} 

अधिक जानकारी के लिए यह लेख पढ़ें: http://www.jerriepelser.com/blog/paging-in-aspnet-webapi-http-headers/

+0

मैं यह कर रहा हूं, लेकिन शीर्षलेख – weagle08

+1

@ weagle08 छीन लिया जाता है क्या आपका अनुरोध प्रॉक्सी के माध्यम से जाता है? यदि ऐसा है तो आप इसे पढ़ सकते हैं: http://stackoverflow.com/questions/20820572/under-what-conditions-are-http-request-headers-removed-by-proxies – Seagull

+0

मेरे लिए काम किया लेकिन हमारे कनेक्शन में कोई प्रॉक्सी शामिल नहीं है – Darren

0

क्या आप आवश्यकता है:

public async Task<IHttpActionResult> Get() 
{ 
    var response = Request.CreateResponse(); 
    response.Headers.Add("Lorem", "ipsum"); 

    return base.ResponseMessage(response); 
} 

मुझे आशा है कि यह आपके प्रश्न का उत्तर देगा।

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