不带正则表达式的ASP.NET Core 2.1中的URL重写 [英] URL rewriting in ASP.NET Core 2.1 without regex
问题描述
我的收藏夹图标位于/images/favicon.ico
,而不是根目录.在Startup.cs
中,我可以添加重定向以避免404:
My favicon is at /images/favicon.ico
rather than the root. In Startup.cs
I can added a redirect to avoid a 404:
app.UseRewriter(new RewriteOptions()
.AddRedirect("favicon.ico", "images/favicon.ico"));
这很好用,但是当然要依赖重定向. 文档还显示了如何使用重写,因此用户看不到重定向.
That works nicely, but of course relies on a redirect. The docs also show how to use rewriting, so the user doesn't see a redirect.
我尝试了这个,但是得到了404:
I tried this, but get a 404:
app.UseRewriter(new RewriteOptions()
.AddRewrite("favicon.ico", "images/favicon.ico", skipRemainingRules:true));
所以我认为也许我需要使用一个虚拟正则表达式:
So I thought that maybe I need to use a dummy regex:
app.UseRewriter(new RewriteOptions()
.AddRewrite("^(favicon.ico)$", "images/favicon.ico", skipRemainingRules:true));
但是我仍然得到404.我该如何进行这项工作?
But I still get a 404. How do I make this work?
推荐答案
根据 Ionix的答案,重写规则必须在调用UseStaticFiles()
As per Ionix's answer, the rewrite rules must be applied before the call to UseStaticFiles()
旧答案,不再相关
按照您的问题,我已经使用AddRewrite
方法进行了很多测试,并且尝试了许多受
Following your question I've done a lot of testing with the AddRewrite
method and I've tried a lot of things inspired by the official documentation but nothing worked. At best I got a redirect ...
所以我的答案是:你不能!仅重定向有效.如果您找到一种方法来进行真正的重写,请告诉我,但就目前而言,我认为这是不可能的.
So my answer is: you can't ! Only a redirect works. If you find a way to make a true rewrite work, please let me know but for now I'll consider this is impossible.
为什么不简单地使用有效的解决方案重定向?您需要重写吗?
Why not simply use the working solution, the redirect ? Do you need a rewrite ?
这篇关于不带正则表达式的ASP.NET Core 2.1中的URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!