【发布时间】:2022-01-16 18:35:33
【问题描述】:
我在基于 ASP.NET MVC 的应用程序中工作,我遇到了这个问题,当我在方法中创建 RedirectToAction 时,它会更改 ControllerBase 类的 Request 属性中的 DateTime 格式。
例如:
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 格式的 ControllerBase 类的 Request 属性的 QueryString。
【问题讨论】:
-
DateTime 根本没有任何格式,它只是一个长数字。格式取决于 DateTime 实例从 DateTime 转换为字符串的方式。
标签: c# asp.net asp.net-mvc datetime redirecttoaction