using System.IO;
/// <summary>
/// WebApi返回图片
/// </summary>
public HttpResponseMessage GetQrCode()
{
var imgPath = @"D:\ITdosCom\Images\itdos.jpg";
//从图片中读取byte
var imgByte = File.ReadAllBytes(imgPath);
//从图片中读取流
var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));
var resp = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(imgByte)
//或者
//Content = new StreamContent(stream)
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
return resp;
}
/// <summary>
/// WebApi返回json数据
/// </summary>
public HttpResponseMessage GetQrCode()
{
var jsonStr = "{\"IsSuccess\":true,\"Data\":\"www.itdos.com\"}";
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonStr, Encoding.UTF8, "text/json")
};
return result;
}
/// <summary>
/// WebApi返回字符串
/// </summary>
public HttpResponseMessage GetQrCode()
{
var str = "IT大师www.itdos.com";
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(str, Encoding.UTF8, "text/plain")
};
return result;
}
//返回html页面
public HttpResponseMessage Get(string id)
{
if (id.Contains("OID="))
{
id = id.Replace("OID=", "");
}
else if (id.Contains("oid="))
{
id = id.Replace("oid=", "");
}
else
{
id = "Not Found!";
}
string html = getHtml(id);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(html, Encoding.UTF8, "text/html")
};
return result;
}
WebApi返回各种类型(图片/json数据/字符串)
最新推荐文章于 2024-07-23 14:32:03 发布