【问题标题】:How to map routes http://localhost:53244/page.aspx?ID=7842 to http://localhost:53244/Home如何将路由 http://localhost:53244/page.aspx?ID=7842 映射到 http://localhost:53244/Home
【发布时间】:2019-08-24 17:56:30
【问题描述】:

需要映射路由,例如 http://localhost:53244/page.aspx?ID=7842http://localhost:53244/Home 并且需要在新的映射 URL 中传递默认查询字符串

我已经尝试过System.Web.Routing.RouteTable.Routes.MapPageRoute("Home", "Home", "~/Page.aspx", false, new RouteValueDictionary { { "ID", "7844" } }); 但它对我不起作用,因为我需要 request.querystring 中的 id 而不是 Page.RouteData.Values["ID"]

这是我的基本代码System.Web.Routing.RouteTable.Routes.MapPageRoute("Home", "Home/{*queryvalues}", "~/Page.aspx", false); 我可以在其中使用http://localhost:53244/Home?ID=7842 之类的网址,但我只需要http://localhost:53244/Home

【问题讨论】:

    标签: c# asp.net .net routing


    【解决方案1】:

    使用 Web.Config 你可以使用 URL 映射

     <system.web> 
        <urlMappings enabled="true">
          <add
              url="~/page.aspx?ID=.."
              mappedUrl="~/Home" />  
      </system.web>
    

    更多信息请查看this

    要处理 page.aspx 返回的所有页面,您可以使用 url 重写规则

    <rewriteModule>
      <rewriteOn>true</rewriteOn>
      <rewriteRules>
          <rule source="(.*)/Home"  
             destination="page.aspx?ID=$1"/> 
      </rewriteRules>
    </rewriteModule>
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多