我想创建一个接受网址的WEB API并返回网址" HTML"页。请问我该怎么做?我相信我的代码错了。我对此很新。感谢*
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
response.Content = new StringContent("https://myurl.com");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
答案 0 :(得分:2)
你的意思是做这样的事情吗?
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
System.Net.WebRequest req = System.Net.WebRequest.Create("https://myurl.com");
using (System.Net.WebResponse resp = req.GetResponse())
using (System.IO.StreamReader sr =
new System.IO.StreamReader(resp.GetResponseStream()))
response.Content = sr.ReadToEnd().Trim();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
答案 1 :(得分:0)
我的 AspNetCore 扩展:
public static ContentResult GetDashboardAsHtml(this AbpController controller)
{
var request = WebRequest.Create($"{controller.Request.Scheme}://{controller.Request.Host}/hangfire");
using var response = request.GetResponse();
using var streamReader = new System.IO.StreamReader(response.GetResponseStream());
return new ContentResult
{
Content = streamReader.ReadToEnd().Trim(),
ContentType = "text/html"
};
}