【发布时间】:2018-03-22 14:46:49
【问题描述】:
我有一个 OWIN 托管应用程序,它可以做 2 件事:
在
mydomain.com/api/...提供API将所有其他请求路由到返回 HTML 页面的单个控制器
我目前有这条路线:
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "{controller}",
defaults: new { controller = "Index" }
);
还有这个控制器:
public class IndexController : ApiController
{
public HttpResponseMessage Get()
{
string html = File.ReadAllText(@"C:/www/.../index.html");
HttpResponseMessage response = new HttpResponseMessage
{
Content = new StringContent(html),
StatusCode = System.Net.HttpStatusCode.OK
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
}
当我去我的家乡路线时,这很好用:
mydomain.com => HTML
如何配置路由模板,以便我总是转到同一个控制器?
mydomain.com/path1 => I want the same HTML
mydomain.com/path1/something => I want the same HTML
mydomain.com/path2 => I want the same HTML
mydomain.com/path3/somethingElse => I want the same HTML
etc
我想要的似乎很简单...我不在乎它是 GET、POST 还是其他。除非路由以api/ 开头,否则我想返回一个 HTML 页面。
有没有使用 Web API 实现此目的的好方法?
=== 编辑
我也在使用静态文件 - 所以静态文件系统的路径应该被明确忽略:
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.IgnoreRoute("StaticFiles", "Public/{*url}");
...
【问题讨论】:
标签: c# asp.net-web-api asp.net-web-api-routing