c# - Parameter Binding in ASP.NET Web API -


i have delegatinghandler in web api authentification (hmac).

i add parameter request return user's id controller.

in handler, tried adding so:

public class securityhandler : delegatinghandler {     protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken)     {         string apikey = request.headers.getvalues(authconfig.apikeyheader).first();         userdto user = userrepo.getuser(apikey);         if (user == null)         {             return sendresponsetext("invalid api key");         }          // validate signature ...          // add user id uri         request.requesturi = new uri(request.requesturi.originalstring + "&userid=" + user.id);         return base.sendasync(request, cancellationtoken);     } } 

in controller, i'm able newly added parameter request uri, parameter binding not working

public class mymodel {     public int userid { get; set; }     ... }  public string get([fromuri] mymodel model) {     // model.userid has not been set (= 0)     // request.requesturi contains "&userid=5" } 

update

i'm guessing binding being done params in httpcontext. tried this, params collection readonly.

var newcontext = request.properties["ms_httpcontext"] httpcontextwrapper; newcontext.request.params.add("userid", "8"); request.properties.remove("ms_httpcontext"); request.properties.add("ms_httpcontext", newcontext); 

i have tried @ side , working.

here sample url.

http://localhost:50382/api/values?userid=10 

here controller action.

public class valuecontroller : apicontroller {     public ienumerable<string> get([fromuri]model my)             {                 return new string[] { "value1", "value2" , my.userid.tostring() };             } } 

as per comment here created delegate handler.

  public class mymessagehandler : delegatinghandler   {     protected override task<httpresponsemessage> sendasync(             httprequestmessage request, cancellationtoken cancellationtoken)     {         var mytempobj = new { id = 20 };       /* have check , remove key. if key present fromuri use cached properties*/         if (request.properties.containskey("ms_querynamevaluepairs"))         {             request.properties.remove("ms_querynamevaluepairs");                         }          // prepare or update uri on here. surely work now.             if (!string.isnullorempty(request.requesturi.query) && request.requesturi.query.contains('?'))             {                 request.requesturi = new uri(request.requesturi + "&userid=" + mytempobj.id);             }             else             {                 request.requesturi = new uri(request.requesturi + "?userid=" + mytempobj.id);             }              return base.sendasync(request, cancellationtoken);     } } 

it work expected.

if original request contain userid duplicated , not work. return 0.

i have doubt using request.requesturi.originalstring. try use request.requesturi. might possible originalstring has encoded value.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -