ASP.NET RedirectToAction 更改请求中的 DateTime 格式

ASP.NET RedirectToAction change DateTime format in request

我在基于 ASP.NET MVC 的应用程序中工作,我遇到了这个问题,当我在一个方法中创建 RedirectToAction 时,它更改了请求 属性 中的 DateTime 格式ControllerBase Class.

例如:

public class MyController:Controller{
    public ActionResult MyController(){
        return RedirectToAction("MyAction","MyController",{Fecha=DateTime.Now});
    }
    public ActionResult MyAction(DateTime date){
        ModelPrueba model = new ModelPrueba(){Fecha=date};
        return View(model);
    }
}

当我调用 MyController 方法时,Request.Params["Fecha"] 例如:30/12/2021 (dd/MM/yyyy).

但是在 RedirectToAction 之后它正在执行 MyAction 方法,Request.Params["Fecha"] 的值类似于 12/30/2021 (MM/dd/yyyy)

有人知道导致此格式更改的原因吗?是否可以不更改格式?

我已经试过了DateTime.ParseExact,但还是不行。

这就像 RedirectToAction 正在使用另一种 DateTime 格式生成 class ControllerBase 的请求 属性 的 QueryString。

您可以使用 string class 来防止这些类型错误。

public class MyController:Controller{
    public ActionResult MyController(){
        return RedirectToAction("MyAction","MyController",{ Fecha = DateTime.Now.ToString("dd/MM/yyyy")});
    }
    public ActionResult MyAction(string date){
        ModelPrueba model = new ModelPrueba(){ Fecha = DateTime.ParseExact(date, "dd/MM/yyyy", new CultureInfo("tr-TR")) };
        return View(model);
    }
}