如何访问SetDefaultContentHeaders基础对象? [英] How to access the underlying object in SetDefaultContentHeaders?

查看:149
本文介绍了如何访问SetDefaultContentHeaders基础对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API,我返回一个对象。当我使用accept头图像/ JPG我要重新那个对象的presentation的形象,但我想设置基于我返回对象的文件名。我实现了一个 BufferedMediaTypeFormatter ,我想我应该在方法做到这一点 SetDefaultContentHeaders 像这样的:

I have a web api where I return a object. When I use the accept header "image/jpg" i want the image representation of that object, but I want to set the file name based on the object I'm returning. I have implemented a BufferedMediaTypeFormatter and thought I should do this in the method SetDefaultContentHeaders like such:

public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
    base.SetDefaultContentHeaders(type, headers, mediaType);
    var myObject = // How do I get this from the response?
    var contentDispositionHeader = new ContentDispositionHeaderValue("attachment") 
        { FileName = myObject.FileName };
    headers.ContentDisposition = contentDispositionHeader;
}

所以,问题是如何获取的潜在对象时,我在 SetDefaultContentHeaders ?我能做到这一点在测试通过阅读它的的Htt presponseMessage 这是在传递给方法,但已被删除。

So the problem is how do I get the underlying object when I am in the SetDefaultContentHeaders? I was able to do it in the beta by reading it from the HttpResponseMessage that was passed in to the method, but that has been removed.

推荐答案

您不能得到的对象实例存在。

You can't get the object instance there.

在格式化的唯一的地方,你可以访问该对象是 WriteToStreamAsync ,到那个时候,你不能再修改标题,因为他们已经发送。

The only place in the formatter where you can access the object is the WriteToStreamAsync, and by that time you can't modify the headers anymore as they are already sent.

您有两种选择,要么通过覆盖 GetPerRequestFormatterInstance request.Properties 和检索格式化C $ C>(因为它运行之前 SetDefaultContentHeaders )。然后,你可以使用 SetDefaultContentHeaders

You have two options, either save the filename in the request.Properties in your controller and retrieve in the formatter by overriding GetPerRequestFormatterInstance (because it runs before SetDefaultContentHeaders). Then you can use this value in SetDefaultContentHeaders

//Controller
public Url Get(int id)
        {
            Request.Properties.Add("name", _repo.Get(id).Name);
            return _repo.Get(id);
}

//Formatter
        public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, MediaTypeHeaderValue mediaType)
        {
            //here save the request.Properties["name"] to some local field which you can use later
            return base.GetPerRequestFormatterInstance(type, request, mediaType);
        }

另外一种是在管道末端使用一个委托处理程序:
即(当然你必须过滤掉当你要反序列化等):

Another is to use a delegating handler at the end of the pipeline: I.e. (of course you have filter out when you want to deserialize and so on):

public class RenameHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
            {
                var msg = t.Result;
                var myobj = msg.Content.ReadAsAsync<IMobi>().Result;
                msg.Content.Headers.ContentDisposition.FileName = myobj.Name + ".mobi";

                return msg;
            });
        }
    }

这篇关于如何访问SetDefaultContentHeaders基础对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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