URL rewriting in ASP.NET Core 2.1 without regex

天大地大妈咪最大 提交于 2019-12-12 21:36:37

问题


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.

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));

But I still get a 404. How do I make this work?


回答1:


As per Ionix's answer, the rewrite rules must be applied before the call to UseStaticFiles()


Old answer, no longer relevant

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 ?



来源:https://stackoverflow.com/questions/51787969/url-rewriting-in-asp-net-core-2-1-without-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!