【问题标题】:How to use Url route url with parameters如何使用带参数的 Url 路由 url
【发布时间】:2016-05-25 13:00:18
【问题描述】:

我有一个带有 ~/x.aspx 和 urlmappings 的页面:

<add url="Home" mappedUrl="~/x.aspx" />

我想要的是当调用 ~/x.aspx?type=y 然后 url 仍然显示 Home 有什么办法吗

<add url="Home" mappedUrl="~/x.aspx" />
 <add url="Home" mappedUrl="~/x.aspx?type=y" />

【问题讨论】:

    标签: asp.net url-routing


    【解决方案1】:

    如果您使用的是 Web 表单,则可以使用以下教程。基本上,“类型”可以是可选复选框的列表,并且可以在您的代码隐藏中构建带有参数的完整 URL。

    Walkthrough: Using ASP.NET Routing in a Web Forms Application

    对于 MVC,请参阅以下问题:

    Routing with Multiple Parameters using ASP.NET MVC

    【讨论】:

      【解决方案2】:

      我没有使用 web.config 中的映射,但显然 is not possibly to use wildcards/regex

      但是您可以通过在 Global.asax 中覆盖 Application_Start 方法来做到这一点

         protected void Application_Start(object sender, EventArgs e)
          {
              RouteConfig.RegisterRoutes(RouteTable.Routes);
          }
      

      RouteConfig.cs

      public class RouteConfig
          {
              public static void RegisterRoutes(RouteCollection routes)
              {
                  // routing segment variable: {}
                  routes.MapPageRoute(null, "home", "~/Pages/x.aspx");
                  routes.MapPageRoute(null, "home/{type}", "~/Pages/x.aspx");
      

      【讨论】:

        猜你喜欢
        • 2017-08-10
        • 1970-01-01
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 2018-08-31
        • 2015-10-06
        • 1970-01-01
        相关资源
        最近更新 更多