【问题标题】:Is it possible to set ContentType for a WCF WebGet method?是否可以为 WCF WebGet 方法设置 ContentType?
【发布时间】:2011-07-11 12:28:57
【问题描述】:

我正在使用返回图像数据流的 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 中,内容类型似乎被标记为应用程序/八位字节流。有没有办法改变内容类型?

【问题讨论】:

    标签: wcf image http rest content-type


    【解决方案1】:

    对不起,我记得我以前做过:

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

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 2011-02-25
      • 2010-10-30
      • 2012-04-21
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多