如何在 asp.net mvc 中将日期时间值作为 URI 参数传递?
我需要一个具有日期时间值的操作参数? 有没有标准的方法来做到这一点? 我需要有类似的东西:
mysite/Controller/Action/21-9-2009 10:20
但我只是成功地做到了类似的东西:
mysite/Controller/Action/200909211020
并编写了一个自定义函数来处理这种格式。
再次,寻找标准或认可的 ASP.net MVC 方法来执行此操作。
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
but I'm only succeeding indoing it with something like:
mysite/Controller/Action/200909211020
and writing a custome function to deal with this format.
Again, looking for a standard or sanctioned ASP.net MVC way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用刻度值。 重建为 DateTime 结构非常简单
Use the ticks value. It's quite simple to rebuild into a DateTime structure
我想我应该向任何寻求类似答案的人分享 MVC5 中对我有用的内容。
我的控制器签名如下所示:
我的 ActionLink 在 Razor 中如下所示:
这给出了如下 URL:
它按应有的方式对日期和时间进行编码。
I thought I'd share what works for me in MVC5 for anyone that comes looking for a similar answer.
My Controller Signature looks like this:
My ActionLink looks like this in Razor:
This gives a URL like this:
Which encodes the date and time as it should.
ASP .NET MVC 的 URI 的典型格式是 Controller/Action/Id,其中 Id 是一个整数,
我建议将日期值作为参数而不是作为路由的一部分发送:
如果它仍然给您带来问题,则日期可能包含字符URI 中不允许且需要进行编码的内容。 查看:
这是 Javascript 中的一个方法。
在服务器端:
仅供参考,只要名称相同,任何 url 参数都可以映射到操作方法参数。
Typical format of a URI for ASP .NET MVC is Controller/Action/Id where Id is an integer
I would suggest sending the date value as a parameter rather than as part of the route:
If it's still giving you problems the date may contain characters that are not allowed in a URI and need to be encoded. Check out:
It is a method within Javascript.
On the Server Side:
FYI, any url parameter can be mapped to an action method parameter as long as the names are the same.
拆分年、月、日、小时和分钟
使用级联 If 语句从传递到操作的参数中构建日期时间
(为 vb.net 道歉,但你明白了:P)
Split out the Year, Month, Day Hours and Mins
Use a cascading If Statement to Build up the datetime from the parameters passed into the Action
(Apologies for the vb.net but you get the idea :P)
从 MVC 5 开始,您可以使用内置的 属性路由 包,该包支持
datetime
类型,它将接受任何可以解析为 DateTime 的内容。例如
更多信息
Since MVC 5 you can use the built in Attribute Routing package which supports a
datetime
type, which will accept anything that can be parsed to a DateTime.e.g.
More info here.
我意识到在后面添加斜线后它就可以工作了
i realize it works after adding a slash behind like so
您应该首先在 global.asax 中添加一个新路由:
在您的控制器上:
记住将默认路由保留在 RegisterRoutes 方法的底部。 请注意,引擎将尝试将您在 {date} 中发送的任何值转换为 DateTime 示例,因此如果无法转换,则会引发异常。 如果您的日期字符串包含空格或 : 您可以对它们进行 HTML.Encode,以便可以正确解析 URL。 如果不是,那么您可以有另一个日期时间表示形式。
You should first add a new route in global.asax:
The on your Controller:
Remember to keep your default route at the bottom of the RegisterRoutes method. Be advised that the engine will try to cast whatever value you send in {date} as a DateTime example, so if it can't be casted then an exception will be thrown. If your date string contains spaces or : you could HTML.Encode them so the URL could be parsed correctly. If no, then you could have another DateTime representation.
我也有同样的问题。 我使用 DateTime.Parse 方法。 并在 URL 中使用此格式传递我的 DateTime 参数 2018-08-18T07:22:16
有关使用 DateTime Parse 方法的更多信息,请参阅此链接:日期时间解析方法
我希望此链接对您有所帮助。
I have the same problem. I use DateTime.Parse Method. and in the URL use this format to pass my DateTime parameter 2018-08-18T07:22:16
for more information about using DateTime Parse method refer to this link : DateTime Parse Method
I hope this link helps you.
第一个示例的网址中的冒号将导致错误(错误请求),因此您无法准确执行您要查找的操作。 除此之外,使用 DateTime 作为操作参数绝对是可能的。
如果您使用默认路由,示例 URL 的第三部分将获取 DateTime 值作为 {id} 参数。 因此,您的 Action 方法可能如下所示:
您可能想像我一样使用 Nullable Datetime,因此如果不包含此参数,也不会导致异常。 当然,如果您不希望将其命名为“id”,则添加另一个路由条目,将 {id} 替换为您选择的名称。
只要 url 中的文本能够解析为有效的 DateTime 值,这就是您所要做的。 像下面这样的东西工作正常,并且会在您的 Action 方法中被拾取,不会出现任何错误:
当然,在这种情况下,问题是我没有包括时间。 我不确定是否有任何方法可以格式化(有效)日期字符串,其中时间不以冒号表示,因此如果您必须在网址中包含时间,您可能需要使用自己的格式并将结果解析回手动输入日期时间。 假设我们将冒号替换为“!” 在操作链接中:
new { id = DateTime.Now.ToString("dd-MM-yyyy HH!mm") }
。您的操作方法将无法将其解析为日期,因此在这种情况下最好的选择可能是将其作为字符串接受:
编辑: 正如评论中所述,还有一些其他解决方案可以说比矿。 当我最初写这个答案时,我相信我正在尝试尽可能保留日期时间格式的本质,但显然 URL 编码将是处理此问题的更正确方法。 对弗拉德的评论+1。
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.
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:
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.
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:
The catch, in this case of course, is that I did not include the time. I'm not sure there are any ways to format a (valid) date string with the time not represented with colons, so if you MUST include the time in the url, you may need to use your own format and parse the result back into a DateTime manually. Say we replace the colon with a "!" in the actionlink:
new { 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:
Edit: 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.
尝试使用 toISOString()。 它返回 ISO8601 格式的字符串。
来自 javascript
来自 c#
Try to use toISOString(). It returns string in ISO8601 format.
from javascript
from c#