如何从web api返回HTML代码 [英] How to return HTML code from web api
问题描述
大家好,
i希望通过使用IhttpActionResult从web api返回html代码
public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@〜/ Template1 / Receipt1.html );
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
//我应该在这里写什么代码
返回Ok();
}
< b>我尝试了什么:
i已经完成
public HttpResponseMessage Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@〜/ Template1 / Receipt1.html);
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
var response = new HttpResponseMessage();
response .Content = new StringContent(sb.ToString());
response.Content.Headers.Content Type = new MediaTypeHeaderValue(text / html);
response.ReasonPhrase =success;
返回回复;
}
以上工作正常,但我想使用IHttpActionResult
public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@〜/ Template1 / Receipt1.html);
string Html = File.ReadAllText( Filepath);
StringBuilder sb = new StringBuilder(Html);
//我应该在这里写什么代码
返回Ok();
}
Hi All,
i want to return html code from web api by using IhttpActionResult
public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
// what code should i write here
return Ok();
}
What I have tried:
i have done
public HttpResponseMessage Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
var response = new HttpResponseMessage();
response.Content = new StringContent(sb.ToString());
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
response.ReasonPhrase = "success";
return response;
}
above is working fine but i want to use IHttpActionResult
public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
// what code should i write here
return Ok();
}
推荐答案
对我来说听起来不是那么古怪:
IHttpActionResult - 在ASP.NET Web API 2中创建响应的新方法StrathWeb。一个自由流动的网络技术独白。 [ ^ ]
Doesn't sound all that "wacky" to me:
IHttpActionResult – new way of creating responses in ASP.NET Web API 2 | StrathWeb. A free flowing web tech monologue.[^]
这篇关于如何从web api返回HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!