【发布时间】:2016-11-01 11:57:43
【问题描述】:
我正在寻找一个 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");
}
如果我用错了,有什么更好的方法,你能给我举个例子吗?
带有 .NET 4.52 的 WebApi 2
编辑:嗯,改进了它,但返回的是 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"]}]}
【问题讨论】:
标签: c# iis asp.net-web-api