为什么将此路由参数附加到查询字符串上? [英] Why is this route parameter tacked onto the querystring?

查看:50
本文介绍了为什么将此路由参数附加到查询字符串上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 3应用程序,该应用程序记录用户的计步器条目.用户可以通过访问/Pedometer 查看所有最新的计步器条目,并可以通过访问诸如/Pedometer/2011 的URL按年份,年份/月份或年份/月份/日期进行过滤.>,/Pedometer/2011/08 /Pedometer/2011/08/15 .

I have an ASP.NET MVC 3 application that records a user's pedometer entries. A user can view all most recent pedometer entries by visiting /Pedometer and can filter by year, year/month, or year/month/date by visiting URLs like /Pedometer/2011, /Pedometer/2011/08 and /Pedometer/2011/08/15, respectively.

我已经在 Global.asax 中创建了两个映射的路由.如下所示,第一种途径是允许各种URL模式按日期进行过滤.第二个路由(未显示)是默认的ASP.NET MVC路由.

I've created two mapped routes in Global.asax. The first route, shown below, is what allows the various URL patterns for filtering by date. The second route (not shown) is the default ASP.NET MVC route.

routes.MapRoute(
    "PedometerEntries", // Route name
    "Pedometer/{year}/{month}/{day}", // URL with parameters
    new
    {
        controller = "Pedometer",
        action = "Index",
        year = UrlParameter.Optional,
        month = UrlParameter.Optional,
        day = UrlParameter.Optional
    }, // Parameter defaults
    new
    {
        year = @"\d{4}",
        month = @"([012]?\d{1})?",
        day = @"(([1-9])|([12][0-9])|(3[0-1]))?"
    } // Parameter constraints
);

这是我的问题.我有一个视图,我想在其中创建以下形式的链接: currentUrl?format = csv ,该链接将允许用户以CSV格式下载所请求URL的计步器条目.因此,如果用户正在访问/Pedometer ,则下载链接将指向/Pedometer?format = csv .如果用户正在访问/Pedometer/2011/08 ,则下载链接将指向/Pedometer/2011/08?format = csv .

Here's my question. I have a view where I want to create a link of the form: currentUrl?format=csv, which will let the user download the pedometer entries for the requested URL in a CSV format. So if a user is visiting /Pedometer, the download link would be to /Pedometer?format=csv. If the user is visiting /Pedometer/2011/08 the download link would be to /Pedometer/2011/08?format=csv.

要创建这样的链接,我添加了一个名为 DownloadToExcel 的自定义HTML助手,其中包含以下代码:

To create such a link I added a custom Html Helper named DownloadToExcel with the following code:

public static MvcHtmlString DownloadToExcel(this HtmlHelper helper, string linkText)
{
    RouteValueDictionary routeValues = helper.ViewContext.RouteData.Values;

    // Add the format parameter to the route data collection, if needed
    if (!routeValues.ContainsKey("format"))
        routeValues.Add("format", "csv");

    return helper.ActionLink(linkText,                          // Link text
                                routeValues["action"].ToString(),  // Action
                                routeValues);                      // Route values
}

当我在视图中添加 @ Html.DownloadToExcel()标记时,它会生成一个链接,但这很麻烦-当用户访问最近的条目或按年/月筛选的条目时或年份/月份/日期,它可以按预期工作,但当用户访问年份过滤器URL时则无效.

When I add the @Html.DownloadToExcel() markup in my view, it generates a link, but here's the rub - when the user visits the recent entries or the entries filtered by year/month or year/month/date, it works as expected, but not when the user visits the year filter URL.

以下列表显示了用户访问的URL以及由自定义Html Helper生成的相应URL:

The following list shows the URL the user visits and the corresponding URL generated by the custom Html Helper:

  • 访问:/计步器-下载链接:/Pedometer?format = csv
  • 访问:/Pedometer/2011 -下载链接:/Pedometer?year = 2011& format = csv
  • 访问:/Pedometer/2011/08 -下载链接:/Pedometer/2011/08?format = csv
  • 访问:/Pedometer/2011/08/15 -下载链接:/Pedometer/2011/08/15?format = csv
  • Visiting: /Pedometer - Download link: /Pedometer?format=csv
  • Visiting: /Pedometer/2011 - Download link: /Pedometer?year=2011&format=csv
  • Visiting: /Pedometer/2011/08 - Download link: /Pedometer/2011/08?format=csv
  • Visiting: /Pedometer/2011/08/15 - Download link: /Pedometer/2011/08/15?format=csv

为什么访问/Pedometer/2011 时下载链接是/Pedometer?year = 2011& format = csv 而不是/Pedometer/2011?format = csv ?为什么它不适用于那种情况,却能按年/月和年/月/日的情况工作呢?

Why is it when visiting /Pedometer/2011 the download link is /Pedometer?year=2011&format=csv and not /Pedometer/2011?format=csv? And why does it not work for that one case but works as expected for the year/month and year/month/date cases?

谢谢

推荐答案

此问题很可能是由

This problem is most likely caused by this bug described by Phil Haack on his blog. There's a regression bug introduced in ASP.NET MVC 3 when you have two consecutive optional URL parameters.

这篇关于为什么将此路由参数附加到查询字符串上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆