尝试创建友好的 & 时使用 Url.Action() 时遇到问题“可破解的”网址
我为一个简单的博客定义了以下路线。
routes.MapRoute(
"Blog",
"blog/{year}/{month}/{day}",
new { controller = "Blog", action = "Index" },
new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
);
该网址应该能够“可破解”以完成以下任务:
- http://abc.com/blog/2010->显示全部 2010 年的帖子
- http://abc.com/blog/2010/01 ->节目 2010 年 1 月的所有帖子
- http://abc.com/blog/2010/01/25-> 显示 2010 年 1 月 25 日的所有帖子
我创建了一个可以很好地处理此操作的控制器。但是,我在使用 Url.Action()
在视图中创建链接时遇到问题。
例如这个
var d = new DateTime(2010, 1, 25);
Url.Action("Index", "Blog", new { year=d.Year, month=d.Month, day=d.Day} );
......生成一个像这样的url:
我更希望它生成一个看起来像上面列表中的 url 的 url。
有吗我可以使用 Url.Action()
或 Html.ActionLink()
来生成我想要的格式的网址吗?
I've defined the following route for a simple blog.
routes.MapRoute(
"Blog",
"blog/{year}/{month}/{day}",
new { controller = "Blog", action = "Index" },
new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
);
The url should be able "hackable" to accomplish the following:
- http://abc.com/blog/2010 -> shows all
posts in 2010 - http://abc.com/blog/2010/01 -> shows
all posts in January 2010 - http://abc.com/blog/2010/01/25 ->
shows all posts in January 25th, 2010
I have created a controller which handles this action quite nicely. However I am having trouble creating links in my views using Url.Action()
.
For example this...
var d = new DateTime(2010, 1, 25);
Url.Action("Index", "Blog", new { year=d.Year, month=d.Month, day=d.Day} );
...generates a url like that looks like this:
I would rather like it to generate a url that looks like the urls in the list above.
Is there any way I can use Url.Action()
or Html.ActionLink()
to generate urls in the format I desire?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于您传递给
Url.Action
的月份是一位数月份,因此与路由定义上的月份限制不匹配。约束通常不仅在传入 URL 上运行,而且在生成 URL 时也运行。解决方法是手动调用月份的 .ToString() 并将其格式化为两位数。您当天也需要做同样的事情。对于年份来说这不是问题,因为我们一生中的所有年份都是四位数。
以下是格式化数字的示例代码:
请注意,手动格式化数字时,您必须使用不变区域性,以便不同的区域性和语言不会影响其格式化方式。
您还需要设置月份和日期的默认值,以便 URL 中不需要它们:
并且在控制器操作中检查月份或日期是否为 0,在这种情况下,您应该显示整个月份或全年。
The issue there is that the month you're passing in to
Url.Action
is a single-digit month, and thus doesn't match the month constraint on the route definition. Constraints are typically run not only on incoming URLs but also when generating URLs.The fix would be to manually call .ToString() on the month and format it as a two-digit number. You'll need to do the same for the day as well. For the year it's not an issue since all the years in our lifetimes will be four-digit numbers.
Here's sample code to format numbers:
Please note that when formatting the number manually that you must use the Invariant Culture so that different cultures and languages won't affect how it gets formatted.
You'll also need to set default values for month and day so that they are not required in the URL:
And in your controller action check if the month or day are 0, in which case you should show an entire month or entire year.
我看到您遇到的另一个问题是,您将需要几个具有适当默认值的其他路由条目来处理其他情况。
“http://abc.com/2010”不会匹配“blog/{year}/{month}” /{天}”。这是一条非常具体的路线,需要三个参数(带有约束)来匹配。为了接受这些其他路线,您需要按照以下方式创建其他路线条目:
有几种方法可以处理这种情况,但现在在您的博客控制器和索引操作中,您可以按年份过滤帖子结果,月、日。
简单的例子:
...
The other issue I see you running into is that you'll need several other route entries with appropriate defaults to handle those other scenarios.
"http://abc.com/2010" will not match "blog/{year}/{month}/{day}". That's a very specific route that requires three parameters (with constraints) to match. In order to accept those other routes you'll need to create other route entries along the lines of:
There are a few ways to handle this scenario, but now in your Blog controller and your Index action you can filter the posts results by year, month, day.
Quick example:
...
您不必为所有条件编写新方法。我的意思是你可以用这个来做;
例如。对于 NHibernate。
附:它正在逐步检查参数,年份->月->天。如果用户没有在查询字符串中设置任何参数,它将返回所有博客。
You dont have to write new method for all condition. I mean you can do it with this;
eg. for NHibernate.
ps. It's checking parameters step by step, year -> month -> day. If user doesnt set any parameters in querystring, it will return all blogs.