c# - Asp.Net Web API to get getails of a particular item -
hi every 1 new asp.net-web-api
, want details of particular item item_code has passed in header of postman or in json format.
i did follows
public list<product> getbycode(string itemcode) { using (var context = new dbcontext()) { var getitem = (from s in context.objproduct (s.itemcode == itemcode) select s).tolist(); if (getitem!=null ) { return getitem; } else { return null; } } }
if doing through query string
localhost:50787/api/product/getbycode?itemcod=3f-47-ab-84-9f-eb-d6-6b-9c-62-cc-85-98-4d-28-6b
as working fine. want has passed through json or header key , values.
please me.
first need post request end point. need edit action [httppost]
attribute.
[httppost] public list<product> getbycode(string itemcode) {...}
i suggest changing name of action posting getbycode
can cause confusion name.
with done, lets focus on how send data endpoint.
sending post action
post localhost:50787/api/product/getbycode?itemcod=3f-47-ab-84-9f-eb-d6-6b-9c-62-cc-85-98-4d-28-6b http/1.1 user-agent: fiddler host: localhost:50787
will work, state
but want has passed through json or header key , values.
passing through header require lot more work on part.
take request example code sent in custom header.
post localhost:50787/api/product/getbycode http/1.1 user-agent: fiddler host: localhost:50787 itemcode: 3f-47-ab-84-9f-eb-d6-6b-9c-62-cc-85-98-4d-28-6b
you have customize api specific header key , map intended action parameter. more work need do.
to send json body , have still call action simple type, need update action know how deal request.
[httppost] public list<product> getbycode([frombody]string itemcode) {...}
take note of [frombody]
attribute.
using [frombody]
to force web api read simple type request body, add [frombody] attribute parameter:
in above example, web api use media-type formatter read value of itemcode
request body. here example client request.
post localhost:50787/api/product/getbycode http/1.1 user-agent: fiddler host: localhost:50787 content-type: application/json content-length: 49 "3f-47-ab-84-9f-eb-d6-6b-9c-62-cc-85-98-4d-28-6b"
Comments
Post a Comment