c# - Obtain the value of a List<T> inside a custom IValueProvider ASP.NET MVC 5.2.3 -


after struggling issue 2 days, decided come here , ask question - best clear can.

first of all, trying pretty simple: implement custom ivalueprovider decrypts parameters of action inside asp.net mvc controller - part done , it's working perfectly; issue starts on moment when have list among action parameters because i'm not able exact values of list request context.

let's take step step.

first of all, have attribute decorates required actions:

public class encryptedquerystringvaluesattribute : filterattribute, iauthorizationfilter {     /// <summary>     /// called when authorization required.     /// </summary>     /// <param name="filtercontext">the filter context.</param>     public void onauthorization(authorizationcontext filtercontext)     {         filtercontext.controller.valueprovider = new encryptedquerystringvaluesprovider(             filtercontext.requestcontext, filtercontext.actiondescriptor);     } } 

next, have value provider constructor i'm initializing need:

public encryptedquerystringvaluesprovider(requestcontext requestcontext, actiondescriptor actiondescriptor) {     this.requestcontext = requestcontext;     this.actiondescriptor = actiondescriptor;      this.prefixcontainer =         new lazy<prefixcontainer>(             () => new prefixcontainer(this.requestcontext.httpcontext.request.params.allkeys), true); } 

as side note, prefixcontainer works charm - if i'm checking if prefix there in container, it's working regardless of prefix type (meaning doesn't matter if parameter collection or simple parameter).

the checking of existence of parameter inside value provider done this:

public bool containsprefix(string prefix) {     return this.prefixcontainer.value.containsprefix(prefix); } 

as i've said - gives me correct values.

now, comes ugly part - getvalue method:

public valueproviderresult getvalue(string key) {     string[] rawvalue = this.requestcontext.httpcontext.request.params.getvalues(key);     string attemptedvalue = this.requestcontext.httpcontext.request.params[key];     return new valueproviderresult(rawvalue, attemptedvalue, cultureinfo.currentculture); } 

the rawvalue extracted right null , logical null because there no key name in request - have there collection this:

[25]: "supplierinvoices[x].paymenttoinvoiceexchangerate" [26]: "supplierinvoices[x].amounttopayongiveninvoice" [27]: "supplierinvoices[x].openinvoiceid" [28]: "supplierinvoices[x].openinvoicetimestamp" 

on other hand, i'm aware have use this.prefixcontainer.value.getkeysfromprefix(prefix); construction obtain keys in request , based on that, somehow join these keys , gave them valueproviderresult, have no idea how! :-)

please, there can explain how these values can joined in order passed valueproviderresult correctly interpreted?

thank you!

after fighting issue last weeks, decided should change approach , try not re-invent wheel. first of all, should drop have related collection processing , these things - this.prefixcontainer should go - if cannot find simple key in httpcontext, should not process , leave rest of providers.

here comes issue - how can "tell" value provider "let go" key??? thought if return false in containsprefix method, processing switch next ivalueprovider in queue - not case - both containsprefix method should return null , more this, getvalue method should return null , under these circumstances, processing move next ivalueprovider.

ok - have these things in place, how can use value provider without valueproviderfactory registered globally because, i've said, processing takes place action "signed" given attribute. answer - instead of instantiating custom valueprovider in controller using attribute code this:

filtercontext.controller.valueprovider = new encryptedquerystringvaluesprovider(filtercontext.requestcontext, filtercontext.actiondescriptor); 

i have add value provider top of list:

valueprovidercollection valueprovidercollection = filtercontext.controller.valueprovider valueprovidercollection; if (valueprovidercollection == null) throw new nullreferenceexception("filtercontext.controller.valueprovider valueprovidercollection");  valueprovidercollection.insert(0, new encryptedquerystringvaluesprovider(filtercontext.requestcontext, filtercontext.actiondescriptor)); 

i hope someone. :-)


Comments