每条路线的格式化配置中的Web API [英] Per Route Formatter Configuration in Web API

查看:123
本文介绍了每条路线的格式化配置中的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题或多或少说明了一切。
我想配置JSON MediaTypeFormatter表现不同每条路线。

The title more or less says it all. I am trying to configure the JSON MediaTypeFormatter to behave differently per route.

具体来说,我有我的WebAPI两条路由映射到相同的控制器。
每个路由执行相同的操作,并返回相同的数据,但与现有的消费者向后可比性的原因,他们必须稍有不同的格式输出。

Specifically I have two routes in my WebAPI that are mapped to the same controller. Each route performs the same operation and returns the same data but for reasons of backwards comparability with existing consumers they must format their output slightly differently.

我可以把一些code的控制器,以确定是否请求排在传统路线或新的路线,并相应地改变格式化。

I could put some code in the Controller to determine if the request came in on the legacy route or the new route and change the formatters accordingly.

我也可以使用一个ActionFilter以更改所需的格式化。

I could also use an ActionFilter to change the formatters where required.

我不过想知道是否有在每路径级别配置格式化,因为这是一个抽象的在我的API行为有所不同水平的一种方式。这既可以是在路由配置的还是在一个委托处理程序的地步。

I was however wondering if there is a way to configure formatters at a per route level because that is the level of abstraction where my API behaves differently. This could either be at the point of Route Configuration or in a Delegate Handler.

有什么建议?

推荐答案

我不完全确定你的两个JSONs多少不同的是,你与他们做什么,但如果你问我,我会做在格式化:

I'm not entirely sure how much different your two JSONs are and what exactly you are doing with them, but if you ask me, I'd do it in the formatter:

public class MyJsonMediaTypeFormatter : JsonMediaTypeFormatter
{
    private IHttpRouteData _route;

    public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, System.Net.Http.Headers.MediaTypeHeaderValue mediaType)
    {
        _route = request.GetRouteData();
        return base.GetPerRequestFormatterInstance(type, request, mediaType);
    }

    public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        if (_route.Route.RouteTemplate.Contains("legacy"))
        {
            //here set the SerializerSettings for non standard JSON
            //I just added NullValueHandling as an example
            this.SerializerSettings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
        }

        return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
    }
}

您会再与这一个替换默认JsonMEdiaTypeFormatter。

You would then replace the default JsonMEdiaTypeFormatter with this one.

    config.Formatters.RemoveAt(0);
    config.Formatters.Insert(0, new MyJsonMediaTypeFormatter());

在网页API你就可以有 DelegatingHandler ,只有特定的路线上运行,但并没有真正意义,因为格式化程序收藏是全球性的,所以没有点修改,在运行时即使从路线范围的处理程序。

In Web API you can have DelegatingHandler that only runs on a specific route, but that doesn't really make sense since Formatters collection is global so there is no point in modifying that in runtime even from a route-scoped handler.

这篇关于每条路线的格式化配置中的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆