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用户注册页
- 下一篇: 微型计算机技术实验报告,微型计算机技术及