I have the need to create a REST webservice. For that I followed this tutorial: http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/
Everything worked fine, I also added BasicAuth to it, it works like a glove.
Now, my question... This webservice will work with possible versions of it, so we decided to implement a sort of versions systems. Also, we want the client applications to choose the database that they want to perform their actions. For that, we thought that it would be nice to have URIs with this style:
http://localhost/Connection/northwind/API/1/DataRow
This is the code that I have. I used to have only the entity DataRow defined. Now I have also defined the entity API and Connection.
How do I implement a URI/endpoint like what I want? This is the code that I have, so 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 to 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; }
}
}