处理/筛选坏/断开的路由/链接-重定向到现有已知路由

本文关键字:路由 重定向 链接 筛选 断开 处理 | 更新日期: 2023-09-27 18:28:16

我有以下路线:

http://somehost/Project/Page/5/Contactgfmsdiojfdui9m 

我希望它被永久重定向到:

http://somehost/Project/Page/5/Contact

如何过滤我不想支持的坏路由?

处理/筛选坏/断开的路由/链接-重定向到现有已知路由

两个选项:

Url重写(文档)

<?xml version="1.0" encoding="UTF-8"?> 
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ProjectPageContent_Redirect" stopProcessing="true">
                    <match url="^/Project/Page/('d+)/Content.*" />
                    <action type="Redirect" url="/Project/Page/{R:1}/Content" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

来自控制器的RedirectPermanent

RedirectController.cs

public class RedirectController : Controller
{
    public ActionResult ProjectPageContent(Int32 id)
    {
        return RedirectPermanent(String.Format("/Project/Page/{0}/Content", id));
    }
}

RouteConfig.cs

routes.MapRoute(
    name: "ProjectPageContent_Redirect",
    url: "/Project/Page/{id}/Contact{*extra}",
    defaults: new { controller = "Redirect", action = "ProjectPageContent" }
);