Asp.Net Web Api - attribute for not binding/formatting a parameter? -


i have method on apicontroller looks this:

public ienumerable<items> getslidesets() {         iserviceclass serviceclass = new serviceclass();         //... 

yes, aware not design i'm addressing issue in different iteration.

at point in application need call functionality within project thought reuse controller (and why not, can pluck out of ioc container). problem in case, need inject own implementation of iserviceclass, easy enough:

public ienumerable<items> getslidesets(iserviceclass serviceclass = null) {         serviceclass = serviceclass ?? new serviceclass();         //... 

except getting errors when calling via regular api call optionalparameter 'serviceclass' not supported formatterparameterbinding.

i know there various attributes control bindings. there 1 can put on parameter shouldn't bind.

like others have mentioned, it's better idea inject dependency in constructor.

but if must avoid binding action parameter, there isn't built-in attribute can create 1 pretty easily. here's like:

public class dontbindattribute : parameterbindingattribute {     public override httpparameterbinding getbinding(httpparameterdescriptor parameter)     {         return new dontbindparameterbinding(parameter);     }      private class dontbindparameterbinding : httpparameterbinding     {         public dontbindparameterbinding(httpparameterdescriptor parameter) : base(parameter)         {         }          public override task executebindingasync(modelmetadataprovider metadataprovider, httpactioncontext actioncontext, cancellationtoken cancellationtoken)         {             actioncontext.actionarguments.add(descriptor.parametername, descriptor.defaultvalue);             var completedtasksource = new taskcompletionsource<object>();             completedtasksource.setresult(null);             return completedtasksource.task;         }     } } 

you need apply attribute parameter afterwards:

public ienumerable<items> getslidesets([dontbind] iserviceclass serviceclass = null) 

Comments