webapi添加html页面,如何从WebApi动作返回html页面?
disassemble-number-5
24
c#
iis
asp.net-web-api
我正在尋找一個WebApi示例,其中默認路由將給定調用者返回給定的html頁面.我的路線和行動設置如下.我只想向他發送index.html頁面,而不是重定向,因為他在正確的位置.
http://localhost/Site // load index.html
// WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "Root",
routeTemplate: "",
defaults: new { controller = "Request", action = "Index" }
);
// RequestControlller.cs
[HttpGet]
[ActionName("Index")]
public HttpResponseMessage Index()
{
return Request.CreateResponse(HttpStatusCode.OK, "serve up index.html");
}
如果我使用這個錯誤,那么更好的方法是什么,你能指點我一個例子嗎?
WebApi 2與.NET 4.52
編輯:嗯,改進了它,但得到了json頭而不是頁面內容.
public HttpResponseMessage Index()
{
var path = HttpContext.Current.Server.MapPath("~/index.html");
var content = new StringContent(File.ReadAllText(path), Encoding.UTF8, "text/html");
return Request.CreateResponse(HttpStatusCode.OK, content);
}
{"Headers":[{"Key":"Content-Type","Value":["text/html; charset=utf-8"]}]}
總結
以上是生活随笔為你收集整理的webapi添加html页面,如何从WebApi动作返回html页面?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户添加页面html,HTML用户注册页
- 下一篇: html 文本框 无法输入,无法在HTM