In the application service API
update is a very common thing, in order to serve working well require a lot of old and new versions compatible; To address the needs of this series of FastHttpApi
enhanced in the new version of the Url rewriting mechanism to support API
access control version, the original fixed rewrite rule rewriting rules to support dynamic adjustment of data, different values of the main support domain name, header, and the like into a rewritable querystring defined. Then explain how FastHttpApi
to control the different versions of API
Access
API examples
[Controller(BaseUrl = "Api")] public class Api { public object Hello(string name) { return $"hello {name} at {DateTime.Now}"; } } [Controller(BaseUrl = "Api/v2")] public class ApiV2 { public object Hello(string name) { return $"hello {name} at {DateTime.Now}[v2]"; } }
These are two different versions of the API function, Url access respectively /Api/hello
and/Api/v2/hello
In fact, many times without changing Url want to be a path of access to different versions, so the adjustment is more convenient and simple to use. FastHttpApi
Supported by domain name, request headers and request parameters to deal with different situations rewritten, so that you can let the caller can not access the path of unity concerns related parameters can be set.
Based domain rewrite
Component supports according to the domain name to rewrite Url, for which a case can be defined V2.beetlex.com
to access /Api/v2/hello
, join in when the program starts a rewrite
static void Main(string[] args) { var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { services.UseBeetlexHttp(o => { o.AddFilter<DefaultJsonResultFilter>(); o.LogToConsole = true; o.Port = 80; o.SetDebug(); o.LogLevel = BeetleX.EventArgs.LogType.Info; }, b => { b.UrlRewrite .Add("v2.beetlex.com", "/api/{action}", "/api/v2/{action}"); }, typeof(Program).Assembly); }); builder.Build().Run(); }
Long by V2.beetlex.com
access /api/{action}
rewritten to /api/v2/{action}
, the access result rewritten as follows:
Normally rarely used to distinguish the domain name, the domain name to distinguish the field is more formal and API compatibility test deployment, will replace some of API principle after completion of the test; the back will tell how old a request to take over the new version.
Header QueryString value basis or to rewrite
In fact, it is added to the Header in the whole Url is no effect, the components can be defined as the rewrite rule
static void Main(string[] args) { var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { services.UseBeetlexHttp(o => { o.AddFilter<DefaultJsonResultFilter>(); o.LogToConsole = true; o.Port = 80; o.SetDebug(); o.LogLevel = BeetleX.EventArgs.LogType.Info; }, b => { b.UrlRewrite .Add("version=v2", "/api/{action}", "/api/v2/{action}") }, typeof(Program).Assembly); }); builder.Build().Run(); }
The above is a configuration version
variable, as long as the variable is equal to v2
the case where the rewrite rule is triggered; head assembly will start with obtaining request to obtain the case data can not be re-acquired from the request Url. Use effect is as follows:
Because the browser is not convenient to add header, add up version of the test by Postman
Replace version
If the service is a case of replacement of old and new versions can be overridden Url, to visit the old api api path to the new path.
static void Main(string[] args) { var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { services.UseBeetlexHttp(o => { o.AddFilter<DefaultJsonResultFilter>(); o.LogToConsole = true; o.Port = 80; o.SetDebug(); o.LogLevel = BeetleX.EventArgs.LogType.Info; }, b => { b.UrlRewrite .Add("/api/{action}", "/api/v2/{action}"); }, typeof(Program).Assembly); }); builder.Build().Run(); }
remind
In fact UrlRewrite component is to support dynamic run-time updates, you only need to be a management page at any time to adjust different versions of the API access process.
Learn more framework webapi functions can be accessed https://github.com/IKende/FastHttpApi/wiki