REST Web服务WebAPI - 基于多个实体创建端点

时间:2016-08-08 12:04:50

标签: c# web-services rest asp.net-web-api odata

我需要创建一个REST Web服务。 为此我遵循了本教程:http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/

一切都很好,我还添加了BasicAuth,就像手套一样。

现在,我的问题...... 这个web服务将与它的可能版本一起使用,因此我们决定实现一种版本系统。此外,我们希望客户端应用程序选择他们想要执行其操作的数据库。 为此,我们认为拥有这种风格的URI会很好:

http://localhost/Connection/northwind/API/1/DataRow

这是我的代码。我以前只定义了DataRow实体。现在我还定义了实体API和Connection。

如何实现我想要的URI /端点? 到目前为止,这是我的代码。

文件: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;
        }
    }
}

控制器\ 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");
        }
    }
}

控制器\ 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");
        }
    }
}

模型\ 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; }
    }
}

2 个答案:

答案 0 :(得分:3)

您可以使用带有属性路由的全局路径前缀配置API版本控制...您可以创建一个继承自virtualenv的类,如下所示

DefaultDirectRouteProvider

并将其添加到WebApiConfig.cs中,如下所示

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); } }

有关详细信息,您可以通过此链接获得帮助... http://www.strathweb.com/2015/10/global-route-prefixes-with-attribute-routing-in-asp-net-web-api/

答案 1 :(得分:1)

只是向你抛出一个离不开的想法:如何使用依赖注入来使用DI容器或容器定义的工厂接口来创建控制器使用的版本化API的实例? / p>

public interface IApiVersion {
    //the public method signatures
}

public interface IApiVersionFactory {
    IApiVersion Create(int version);
}

将工厂注入控制器并调用正确版本化API的方法,你就可以了。