0

我在 Global.asax 页面中将此代码用于 asp.net 4 中的 Map Page Route

    protected void RegistreRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute(
        "Lerning-browse", "Learning-CSharp", "~/CSharp.aspx");
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        RegistreRoutes(System.Web.Routing.RouteTable.Routes);
    }

当用户在 URL 中输入mysite.com/Learning-CSharp时。MapPageRoute 其工作并获取mysite.com/Learning-CSharp URL。但是如果用户在 URL 中输入mysite.com/CSharp.aspx ,则它的 Working 并在 URL 中获取mysite.com/CSharp.aspx

我不想在 URL 中获取mysite.com/CSharp.aspx 。我想如果用户在 URL 中输入mysite.com/CSharp.aspx将其转换为mysite.com/Learning-CSharp

4

3 回答 3

0
protected void RegistreRoutes(System.Web.Routing.RouteCollection routes)
{
    routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
    routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
    routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
    routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
    routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new              
    System.Web.Routing.StopRoutingHandler()));
    routes.Add(new System.Web.Routing.Route("{resource}.js/{*pathInfo}", new 
    System.Web.Routing.StopRoutingHandler()));

    routes.MapPageRoute(
         "HomeRoute",
         "",
         "~/default.aspx"
     );
    routes.MapPageRoute(
    "Lerning-browse", "Learning-CSharp", "~/CSharp.aspx");
}

protected void Application_Start(object sender, EventArgs e)
{
    RegistreRoutes(System.Web.Routing.RouteTable.Routes);
}
于 2013-09-15T10:34:23.973 回答
0

这是 IIS URL 重写的工作。

http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

应该按照以下方式进行操作:

<rewrite>
    <rules>
        <rule name="Rewrite to article.aspx">
            <match url="(.*)(\.aspx)$" />
            <action type="Rewrite" url="{R:1}" />
        </rule>
    </rules>
</rewrite>

如果您还想支持查询字符串,则需要进一步修改。

于 2013-09-15T10:35:30.150 回答
0

如果您只需要管理一个或两个路由,一个更简单的过程是将它们作为硬编码重定向简单地添加到您的 web.config 文件中。

就像是:

<location path="CSharp.aspx">
    <system.webServer>
        <httpRedirect enabled="true" destination="/Learning-CSharp" exactDestination="true" httpResponseStatus="Permanent" />
    </system.webServer>
</location>

我在许多旧网站上使用过这种类型的重定向,尤其是在迁移到新的代码库时;即从webforms 迁移到MVC。

希望这可以帮助。

于 2013-09-15T22:54:11.667 回答