处理/过滤坏/损坏的路由/链接 - 重定向到现有的已知路由

时间:2013-08-19 12:36:15

标签: c# asp.net-mvc asp.net-mvc-routing http-redirect

我有以下路线:

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

我希望将其永久重定向到:

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

如何完成我不想支持的错误路由过滤?

1 个答案:

答案 0 :(得分:3)

两个选项:

Url重写(Documentation

<?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>
来自Controller的

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