【问题标题】:Creating urls with asp.net MVC and RouteUrl使用 asp.net MVC 和 RouteUrl 创建 url
【发布时间】:2010-09-26 20:39:08
【问题描述】:

我想获取当前 URL 并将附加参数附加到 url(例如 ?id=1)

我已经定义了一条路线:

        routes.MapRoute(
            "GigDayListings",                                   // Route name
            "gig/list/{year}/{month}/{day}",                    // URL with parameters
            new { controller = "Gig", action = "List" }         // Parameter defaults
        );

        In my view I have a helper that executes the following code: 

        // Add page index
        _helper.ViewContext.RouteData.Values["id"] = 1;

        // Return link
        var urlHelper = new UrlHelper(_helper.ViewContext);
        return urlHelper.RouteUrl( _helper.ViewContext.RouteData.Values);

但是这不起作用。

如果我的原始网址是: 演出/列表/2008/11/01

我明白了

gig/list/?year=2008&month=11&day=01&id=1

我希望网址为: 控制器/动作/2008/11/01?id=1

我做错了什么?

【问题讨论】:

    标签: asp.net-mvc model-view-controller routes


    【解决方案1】:

    规则的顺序是有意义的。尝试先插入此规则。

    如果需要,也不要忘记定义约束 - 这将导致更好的规则匹配:

    routes.MapRoute(
        "GigDayListings",                                // Route name
        "gig/list/{year}/{month}/{day}",                // URL with parameters
        new { controller = "Gig", action = "List" },    // Parameter defaults
        new
            {
                year = @"^[0-9]+$",
                month = @"^[0-9]+$",
                day = @"^[0-9]+$"
            }    // Constraints
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 2013-04-12
      • 2011-12-07
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多