c# - REST webservice WebAPI - create endpoint based on multiple entities -
i have need create rest webservice. followed tutorial: http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/
everything worked fine, added basicauth it, works glove.
now, question... webservice work possible versions of it, decided implement sort of versions systems. also, want client applications choose database want perform actions. that, thought nice have uris style:
http://localhost/connection/northwind/api/1/datarow
this code have. used have entity datarow defined. have defined entity api , connection.
how implement uri/endpoint want? code have, far.
file: webapiconfig.cs
using integration.models; using microsoft.odata.edm; using system.web.http; using system.web.odata.batch; using system.web.odata.builder; using system.web.odata.extensions; using integration.controllers; namespace integration { public static class webapiconfig { public static void register(httpconfiguration config) { config.mapodataserviceroute("odata", null, getedmmodel(), new defaultodatabatchhandler(globalconfiguration.defaultserver)); config.ensureinitialized(); } private static iedmmodel getedmmodel() { //globalconfiguration.configuration.filters.add(new basicauthenticationfilter()); // basicautenthentication odataconventionmodelbuilder builder = new odataconventionmodelbuilder(); builder.namespace = "integration"; builder.containername = "defaultcontainer"; builder.entityset<datarow>("datarow"); builder.entityset<connection>("connection"); builder.entityset<api>("api"); var edmmodel = builder.getedmmodel(); return edmmodel; } } }
controllers\datarows.cs
using integration.datasource; using system.linq; using system.web.http; using system.web.odata; using system.net; namespace integration.controllers { [enablequery] public class datarowcontroller : odatacontroller { [basicauthenticationfilter] public ihttpactionresult get() { return content(httpstatuscode.nocontent,"nocontent"); } [basicauthenticationfilter] public ihttpactionresult post(models.datarow row) { if (!modelstate.isvalid) { return badrequest(modelstate); } //do stuff save data // .. return content(httpstatuscode.created, "ok"); } } }
controllers\connections.cs
using integration.datasource; using system.linq; using system.web.http; using system.web.odata; using system.net; namespace integration.controllers { [enablequery] public class connectioncontroller : odatacontroller { [basicauthenticationfilter] public ihttpactionresult get() { return ok(integrationdatasources.instance.connection.asqueryable()); } [basicauthenticationfilter] public ihttpactionresult post(models.connection connection) { return content(httpstatuscode.notimplemented, "notimplemented"); } } }
models\datarow.cs
using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.linq; using system.web; namespace integration.models { public class datarow { [key] public int id { get; set; } [required] public int type { get; set; } [required] public string datatype { get; set; } [required] public string data { get; set; } [required] public int apiversion { get; set; } [required] public string integrationprovider { get; set; } } public class connection { [key] public string connectionname { get; set; } public api api { get; set; } } public class api { [key] public int version { get; set; } public datarow row { get; set; } } }
you can config api versioning global route prefixes attribute routing... can create class inherits defaultdirectrouteprovider
1 below
public class centralizedprefixprovider : defaultdirectrouteprovider { private readonly string _centralizedprefix; public centralizedprefixprovider(string centralizedprefix) { _centralizedprefix = centralizedprefix; } protected override string getrouteprefix(httpcontrollerdescriptor controllerdescriptor) { var existingprefix = base.getrouteprefix(controllerdescriptor); if (existingprefix == null) return _centralizedprefix; return string.format("{0}/{1}", _centralizedprefix, existingprefix); } }
and add in webapiconfig.cs this
config.maphttpattributeroutes(new centralizedprefixprovider("api/v{version:int}"));
for more information, can link... http://www.strathweb.com/2015/10/global-route-prefixes-with-attribute-routing-in-asp-net-web-api/
Comments
Post a Comment