Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NETCore中更改路由中的日期时间格式?_C#_Datetime_Asp.net Core_.net Core_Datetime Format - Fatal编程技术网

C# 如何在ASP.NETCore中更改路由中的日期时间格式?

C# 如何在ASP.NETCore中更改路由中的日期时间格式?,c#,datetime,asp.net-core,.net-core,datetime-format,C#,Datetime,Asp.net Core,.net Core,Datetime Format,我在Route属性中使用datetime,如下所示: [Route("{givenDate:datetime}")] 但它是“美国”格式,即“月-日-年” 如何将其转换为“日-月-年”格式 另外,我知道我可以使用其他格式,如“yearmonthday”或“yearmonthday”,但“day-monthyear”对我来说更直观。您可以使用如下路线: [Route("{day:regex(^[[0-2]][[0-9]]|3[[0-1]]$)}-{month:regex(^0[[0-9]]|1[

我在
Route
属性中使用
datetime
,如下所示:

[Route("{givenDate:datetime}")]
但它是“美国”格式,即“月-日-年”

如何将其转换为“日-月-年”格式


另外,我知道我可以使用其他格式,如“yearmonthday”或“yearmonthday”,但“day-monthyear”对我来说更直观。

您可以使用如下路线:

[Route("{day:regex(^[[0-2]][[0-9]]|3[[0-1]]$)}-{month:regex(^0[[0-9]]|1[[0-2]]$)}-{year:regex(^\\d{{4}}$)}")]
然后将您的操作更改为:

public IActionResult Foo(int day, int month, int year)
{
    var givenDate = new DateTime(year, month, day);

    ...
}

诚然,这样做很糟糕,但它确实完成了任务。regex约束虽然非常难看,但它将确保最终通过的任何值都将用于构造一个
DateTime
对象,该对象可能与问题重复。我不这么认为。我知道要更改日期时间格式,但我不知道如何在
路由
属性:)中设置它。看起来您无法更改
日期时间
约束:请参阅,尤其是链接部分底部的警告。Ohh。。。我想那是一种耻辱。但是谢谢你