如何通过URL传递日期,以便在MVC中读取我的操作? [英] How do I pass a date via the URL, for my Action to read in MVC?
本文介绍了如何通过URL传递日期,以便在MVC中读取我的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过URL传递日期?
How do I pass a date via the URL?
这是我要尝试的操作,因为您会看到日期使用的是正斜杠,而这些斜杠在URL中无效
Here is what I am trying to do, as as you'll see the date uses forward slashes which are invalid in the URL
http://localhost/Controller/Action/id=0d5375aa-6d43-42f1-91f0-ea73d9beb361&date=02/12/2009
推荐答案
您可以使用特定格式(例如yyyymmdd)在查询字符串中传递日期,然后在Controller中正确解析它.
You could pass a date in the query string using a specific format, say yyyymmdd and then parse it correctly in your Controller.
&date=02/12/2009
change to
&date=20091202 (yyyymmdd)
您可以围绕使用此新格式实例化的DateTime对象创建包装器,也可以自己在Controller中对其进行解析.
You could either create a wrapper around the DateTime object that was instantiated using this new format or just parse it yourself in the Controller.
public MyWrapperDate(int date)
{
int year = date / 10000;
int month = ((date - (10000 * year)) / 100);
int day = (date - (10000 * year) - (100 * month));
this.DateTimeObject = new DateTime(year, month, day);
}
这篇关于如何通过URL传递日期,以便在MVC中读取我的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文