ASP.NET Core MVC重写和默认文档

时间:2017-04-18 17:17:43

标签: asp.net-core asp.net-core-mvc asp.net-mvc-routing

我试图设置Core MVC Rewrite:

  • 请求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?}"); });

这就是我现在所拥有的:

    app.UseRewriter(new RewriteOptions().AddRewrite("part(\\/.*)?", "part/index.html", true));

    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseIdentity();
    app.UseMvc(...);

我尝试了不同的设置,没有运气。这甚至可以在ASP.NET Core MVC中使用吗?

更新

这里有种类的代码:

localhost/part/[anything]/[something]

现在,当请求index.html时,它会投放localhost/part/[anything]/[something]/index.html。但它用作{{1}},这是Angular的BASE_HREF的一个问题。因此,应用显示正在加载... ,并且显然没有加载任何其他内容。嗯......这需要Angular的部分修复。我现在不知道修复。任何人吗?

2 个答案:

答案 0 :(得分:0)

是的,可以使用正确的正则表达式:

var options = new RewriteOptions()
    .AddRewrite(@"^part(/)?.*$", "part/index.html", true);
app.UseRewriter(options);

//should be after URL rewrite
app.UseStaticFiles();

P.S。我不是一个正则表达的大师,但让我试试。

答案 1 :(得分:0)

怎么样:

app.UseRewriter(new RewriteOptions().AddRewrite("part(.*)", "part/index.html", true));
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(...);