VersionedRoute Attribute Implementation for MVC6

做~自己de王妃 提交于 2019-12-03 05:53:08

问题


I am trying to enable versioning on a REST API, where the version is specified in the header, as "api-version":2.

According to this tutorial I just need to create

VersionConstraint : IHttpRouteConstraint

and

VersionedRoute: RouteFactoryAttribute

The usage would be to apply the [VersionedRoute("api/controllerName", 2)] Attribute to Controllers, which are designed for specific versions (e.g. version 2 in this case).

This is all good and well, but unfortunately, it's all in MVC5 and I'm using MVC6. Therefore, RouteFactoryAttribute and IHttpRouteConstraint don't work.

I managed to find IRouteConstraint to replace IHttpRouteConstraint (hoping it will work), but I cannot find a replacement for RouteFactoryAttribute.

If anyone can provide a sample of this using MVC 6, or at least mention the correct classes (ideally with namespaces) I need to inherit from?


回答1:


Here's the minimum amount of work you need.

First, go there and copy the code for the 3 following files:

  • VersionGetAttribute
  • VersionRangeValidator
  • VersionRoute.cs

Once you have this, we'll change the GetVersion method of VersionRangeValidator for the following:

public static string GetVersion(HttpRequest request)
{
    if (!string.IsNullOrWhiteSpace(request.Headers["api-version"]))
        return request.Headers["api-version"];

    return "1";
}

That will read the header and return the API version. The default will be v1 in this scenario.

Here's how to use it on controllers (or it can be the same controllers with 2 actions:

[Route("api/data")]
public class DataController 
{
    [VersionGet("", versionRange: "[1]")]
    public string GetData()
    {
        return "v1 data";
    }
}

[Route("api/data")]
public class DataV2Controller 
{
    [VersionGet("", versionRange: "[2]")]
    public string GetData()
    {
        return "v2 data";
    }
}

So now you just need to give it the right header and it's good. This code has been tested with jQuery like this:

$(document).ready(function(){
    $.ajax({url: '/api/Data/', headers: { 'api-version': 1 }})
    .then(function(data){
            alert(data);
            });
    $.ajax({url: '/api/Data/', headers: { 'api-version': 2 }})
    .then(function(data){
            alert(data);
            });
});


来源:https://stackoverflow.com/questions/32892557/versionedroute-attribute-implementation-for-mvc6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!