【发布时间】:2017-09-14 15:55:03
【问题描述】:
我正在尝试将 Core MVC Rewrite 设置为:
- 当
localhost/part被请求时,它以默认的localhost/part/index.html响应 - 当
localhost/part/[anything]时,仍以index.html响应(相同的localhost/part/index.html)
这是我现在拥有的:
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRewriter(new RewriteOptions().AddRewrite("/part(/.*)?$", "index.html", true));
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我尝试了不同的设置,但没有运气。这在 ASP.NET Core MVC 中是否可行?
更新
这是某种工作的代码:
app.UseRewriter(new RewriteOptions().AddRewrite("part(\\/.*)?", "part/index.html", true));
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(...);
所以现在,当请求 localhost/part/[anything]/[something] 时,它确实服务于 index.html。但它作为localhost/part/[anything]/[something]/index.html 服务,这是Angular 的BASE_HREF 的问题。所以应用程序显示 Loading... 并且没有明显加载任何其他内容。嗯...这需要 Angular 部分的修复。我现在不知道修复。有人吗?
【问题讨论】:
-
我建议这样的问题可以简单地简化为“查找正则表达式 X”,
regex标签可能会很有帮助。 -
我实际上认为我的正则表达式是正确的。您提出的解决方案不起作用:(
-
查看我的更新。
-
试试这个 app.UseRewriter(new RewriteOptions().AddRewrite("/part", "index.html", true));
-
没有。它不起作用,也不应该。
标签: asp.net-core asp.net-core-mvc asp.net-mvc-routing