如何传递datetime值在asp.net mvc的一个URI参数? [英] How do I pass a datetime value as a URI parameter in asp.net mvc?

查看:165
本文介绍了如何传递datetime值在asp.net mvc的一个URI参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个具有日期时间值的操作参数?是否有这样做的标准方式?我需要这样的:

I need to have an action parameter that has a datetime value? Is there a standard way to do this? I need to have something like:

mysite/Controller/Action/21-9-2009 10:20

但我只用成功像indoing吧:

but I'm only succeeding indoing it with something like:

mysite/Controller/Action/200909211020

和写的是定制函数来处理这种格式。

and writing a custome function to deal with this format.

再次寻找一个标准或认可的ASP.net MVC的方式来做到这一点。

Again, looking for a standard or sanctioned ASP.net MVC way to do this.

推荐答案

在你的第一个例子中的URL中的冒号会导致错误(错误请求),所以你不能做你正在寻找什么。除此之外,使用日期时间作为动作参数是最绝对有可能。

The colon in your first example's url is going to cause an error (Bad Request) so you can't do exactly what you are looking for. Other than that, using a DateTime as an action parameter is most definitely possible.

如果您使用的是默认的路由,你的榜样网址的这部分第三是要皮卡DateTime值作为{ID}参数。所以你的行动方法可能是这样的:

If you are using the default routing, this 3rd portion of your example url is going to pickup the DateTime value as the {id} parameter. So your Action method might look like this:

public ActionResult Index(DateTime? id)
{
    return View();
}

您可能会想使用一个可空日期时间,因为我有,所以如果不包括此参数,它不会导致异常。当然,如果你不希望它被命名为ID,然后使用您选择的名称添加另一路由条目替换(编号)。

You'll probably want to use a Nullable Datetime as I have, so if this parameter isn't included it won't cause an exception. Of course, if you don't want it to be named "id" then add another route entry replacing {id} with your name of choice.

只要在URL文本将解析为一个有效的DateTime值,这是你必须做的。像下面的内容能正常工作,并在你的行动方式将有所回升没有任何错误:

As long as the text in the url will parse to a valid DateTime value, this is all you have to do. Something like the following works fine and will be picked up in your Action method without any errors:

<%=Html.ActionLink("link", "Index", new { id = DateTime.Now.ToString("dd-MM-yyyy") }) %>

美中不足的,在这种情况下,当然是,我并没有包括时间。我不知道有格式化的(有效)日期字符串的时候不要再用冒号psented $ P $的方式,因此,如果您必须在URL中包含的时候,你可能需要使用自己的格式和解析结果转化成手动将日期时间。假设我们有替代冒号!在ActionLink的:新{ID = DateTime.Now.ToString(!DD-MM-YYYY HH MM)}

您的操作方法将无法解析此作为一个日期,以便在这种情况下,最好的选择可能会接受它作为一个字符串:

Your action method will fail to parse this as a date so the best bet in this case would probably to accept it as a string:

public ActionResult Index(string id)
{
    DateTime myDate;
    if (!string.IsNullOrEmpty(id))
    {
        myDate = DateTime.Parse(id.Replace("!", ":"));
    }
    return View();
}

编辑:正如在评论中指出,还有一些其他的解决方案,可以说比我的好。当我最初写这个答案,我相信我是想preserve日期时间格式的本质是最好的,但显然URL编码这将是处理这个更合适的方式。 +1弗拉德的评论。

As noted in the comments, there are some other solutions arguably better than mine. When I originally wrote this answer I believe I was trying to preserve the essence of the date time format as best possible, but clearly URL encoding it would be a more proper way of handling this. +1 to Vlad's comment.

这篇关于如何传递datetime值在asp.net mvc的一个URI参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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