Asp.net core ASP.NET核心MVC重写和默认文档
我试图将核心MVC重写设置为:Asp.net core ASP.NET核心MVC重写和默认文档,asp.net-core,asp.net-core-mvc,asp.net-mvc-routing,Asp.net Core,Asp.net Core Mvc,Asp.net Mvc Routing,我试图将核心MVC重写设置为: 当请求localhost/part时,它会以默认的localhost/part/index.html 当localhost/part/[anything]时,仍然使用index.html(相同的localhost/part/index.html) 以下是我现在拥有的: app.UseDefaultFiles(); app.UseStaticFiles(); app.UseRewriter(new RewriteOptions().Ad
- 当请求
时,它会以默认的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核心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的一个问题。因此,应用程序显示加载…,并且没有明显加载其他内容。陛下这需要Angular的零件进行修复。我现在不知道该怎么解决。有人吗?是的,可以使用正确的正则表达式:
var options = new RewriteOptions()
.AddRewrite(@"^part(/)?.*$", "part/index.html", true);
app.UseRewriter(options);
//should be after URL rewrite
app.UseStaticFiles();
另外,我不是正则表达式大师,但让我试试。怎么样:
app.UseRewriter(new RewriteOptions().AddRewrite("part(.*)", "part/index.html", true));
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(...);
我建议这样的问题可以简单地简化为“Find regex X”,而
regex
标签可能非常有用。我实际上认为我的regex是正确的。您建议的解决方案无效:(请参阅我的更新。尝试此app.UseRewriter(new RewriteOptions().AddRewrite(“/part”,“index.html”,true));不。它不起作用,也不应该起作用。@alvipeo你确定你可以在任何路径上点击index.html
,而无需URL重写吗?什么是index.html
location?localhost/part
是我的Angular 4应用程序,位于ASP.NET核心MVC项目中。没有重写,我可以转到localhost/part
(它实际上返回/part/index.html
)Angular应用程序正常工作。问题是当我点击localhost/part/users/1
MVC路由开始处理它,显然我得到了404。但我想要的是MVC路由不处理/part/[随便什么]
完全可以。UseStaticFiles
应该在管道中的UseRewriter
之后才能正确处理静态文件请求。我已经更正了正则表达式并得到了一个完全可行的示例。有关详细信息,请参阅答案。不,这也没有帮助。@Alvipo检查服务器日志中如何触发中间件。RewriteMiddleware
places这里有一个重写结果,看看它。不,那也不行。这是我的应用程序http://localhost:22246/part/directory/patients/new
我正在尝试工作。但建议的解决方案都不起作用。请参阅“编辑”,在poc应用程序中验证了这一点。如果不起作用,则存在与重写无关的其他问题e(问题是前导斜杠)