-1

我有以下路线:

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

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

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

我怎样才能完成过滤我不想支持的坏路由?

4

1 回答 1

3

两种选择:

网址重写(文档

<?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从控制器

重定向控制器.cs

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

路由配置.cs

routes.MapRoute(
    name: "ProjectPageContent_Redirect",
    url: "/Project/Page/{id}/Contact{*extra}",
    defaults: new { controller = "Redirect", action = "ProjectPageContent" }
);
于 2013-08-19T13:00:52.047 回答