是否可以为WCF WebGet方法设置内容类型?

时间:2011-03-08 23:50:10

标签: wcf image http rest content-type

我正在使用WCF restful / http方法返回图像数据流。我想确保内容类型标记为“image / png”。该方法定义为:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class TileImageService
{
    [WebGet(UriTemplate = "{id}")]
    public Stream GetTileImage(string id)
    {
        Bitmap bmp = new Bitmap(173, 173);
        Graphics g = Graphics.FromImage(bmp);

        g.Clear(Color.Blue);
        g.DrawString(DateTime.Now.ToLongTimeString(), new Font("Chiller", 20), Brushes.White, new PointF(10, 10));
        g.Flush();

        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Png);

        ms.Seek(0, SeekOrigin.Begin);

        return ms;
    }
}

在Firefox中,内容类型看起来像是application / octet stream。有没有办法改变内容类型?

1 个答案:

答案 0 :(得分:8)

抱歉,我记得以前我曾经做过:

WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; // or anything