asp.net web api - Using OData actions -
versions
to use features $select
, $expand
upgraded packages webapi , odata pre-release versions (from aspnetwebstack).
microsoft.aspnet.webapi -> 5.0.0-beta1-130514
microsoft.aspnet.webapi.client -> 5.0.0-beta1-130514
microsoft.aspnet.webapi.core -> 5.0.0-beta1-130514
microsoft.aspnet.webapi.odata -> 5.0.0-beta1-130514
microsoft.aspnet.webapi.web... -> 5.0.0-beta1-130514
controllers
i have base class api controlers:
public class baseapicontroller<t> : entitysetcontroller<t, int> t: class, ientity, new() { public irepository repositorio { get; private set; } public baseapicontroller(irepository repositorio) { repositorio = repositorio; } [queryable(allowedqueryoptions = allowedqueryoptions.all, pagesize=20)] public override iqueryable<t> get() { return repositorio.query<t>(); } [queryable(allowedqueryoptions = allowedqueryoptions.all)] protected override t getentitybykey(int key) { return repositorio.get<t>(key); } }
and user controller
[authorize] public class usuarioscontroller : baseapicontroller<usuario> { public usuarioscontroller(irepository repositorio) : base(repositorio) { } [authorize(roles="admin,ti")] public httpresponsemessage post(usuario usuario) { var x = websecurity.createuserandaccount(usuario.email, "maisbb", new { nome = usuario.nome }); //todo: não fixar senha repositorio.store(usuario); return request.createresponse(httpstatuscode.ok, usuario); } [httpget, queryable(allowedqueryoptions = allowedqueryoptions.all)] public iqueryable roles([fromodatauri] int key) { var usuario = (from u in repositorio.query<usuario>() u.id == key select new { u.email }).singleordefault(); return roles.getrolesforuser(usuario.email).asqueryable(); } }
goals
an action roles
retrieve roles specific user:
goal retrieve roles of user url:
/api/usuarios(67)/roles
i configured api webapiconfig
follows:
modelbuilder.entityset<usuario>("usuarios"); var entitytypeusuario = modelbuilder.entity<usuario>(); var actroles = entitytypeusuario.action("roles"); actroles.parameter<int>("key"); actroles.returns<string[]>(); ... var model = modelbuilder.getedmmodel(); config.routes.mapodataroute(routename: "odata", routeprefix: "api", model: model); config.enablequerysupport(); var jsonformatter = globalconfiguration.configuration.formatters.jsonformatter; var enumconverter = new stringenumconverter(); jsonformatter.serializersettings.converters.add(enumconverter); config.formatters.remove(config.formatters.xmlformatter); var jqueryformatter = config.formatters.firstordefault(x => x.gettype() == typeof(jquerymvcformurlencodedformatter)); config.formatters.remove(config.formatters.xmlformatter); config.formatters.remove(config.formatters.formurlencodedformatter); config.formatters.remove(jqueryformatter); config.formatters.jsonformatter.serializersettings.dateformathandling = dateformathandling.isodateformat;
error
get: /api/usuarios(67)/role
"this service doesn't support odata requests in form '~/entityset/key/action'."
odataactions should post requests. can try modifying code following , see if resolves issue?
[httppost, queryable(allowedqueryoptions = allowedqueryoptions.all)] public iqueryable roles([fromodatauri] int key, odataactionparameters parameters) { var usuario = (from u in repositorio.query<usuario>() u.id == key select new { u.email }).singleordefault(); return roles.getrolesforuser(usuario.email).asqueryable(); }
also, need not specify parameter 'key' while building model key retrieved uri itself. updated code below:
modelbuilder.entityset<usuario>("usuarios"); var entitytypeusuario = modelbuilder.entity<usuario>(); var actroles = entitytypeusuario.action("roles"); actroles.returns<string[]>();
fyi: can @ nice post mike odata actions: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions
Comments
Post a Comment