【问题标题】:Url routing is not working?网址路由不起作用?
【发布时间】:2012-06-24 15:09:58
【问题描述】:

用于 url 重写的 web.config 是

 <rewrite>
        <rules>
            <rule name="Mobile Portal">
                <match url="^(code)(/)?([^']*)" />
                <action type="Redirect" url="Code.aspx?id={R:3}" />
            </rule>
        </rules>
    </rewrite>


input: www.abc.com/Code.aspx?id=123abcdef
Required output: www.abc.com/code/123abcdef
current output: http://www.abc.com/Code.aspx?id=.aspx

有效的页面网址是 www.abc.com/code/123abc 。我需要 "123abc" 。当我通过 www.abc.com/code/123abc 访问该页面时,该 url 被转换为“http://www.abc.com/Code.aspx?id=. aspx "。我使用的是 IIS 7。那么我该如何解决这个问题?

非常感谢。

【问题讨论】:

    标签: iis-7 url-rewriting web-config


    【解决方案1】:

    您可以使用 url 路由而不是 url 重写。为此,请转到 Global.asax 文件并编写 RegisterRoutes 方法,如下所示:

    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("my_abc_page",
            "code/{id}",
            "~/Code.aspx");
    }
    

    使用上述命令,任何像www.abc.com/code/123abcdef 这样的请求都会解释为www.abc.com/Code.aspx?id=123abcdef,您可以在页面代码中访问此ID:

    theId = Page.RouteData.Values["id"] as string;
    

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 1970-01-01
      • 2017-02-02
      • 2017-01-24
      • 2020-01-26
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多