如何将当前日期时间作为字符串附加到 RouteConfig.cs 中的 URL

问题描述

所以我的 geometry.dart

中有以下路线
RouteConfig.cs

基于此路线,我的 routes.MapRoute( name: "Student",url: "student",defaults: new { controller = "Home",action = "Student",id = UrlParameter.Optional } ); 控制器中有以下方法

Home

这个简单的路由在调用 public ActionResult Student() { return View(); } 时会将我带到学生视图。到目前为止一切都很好。

如何实现这条路线:http://localhost:54326/student 当我自动调用上述路线时?

基本上,我想在调用原始路由时在其末尾附加一个字符串。

我可以在 http://localhost:54326/student/01-28-2021 中指定什么来实现这一目标吗?

解决方法

以下路由 Student 将允许在调用时在 http://localhost:54326/student 的末尾附加一个字符串。

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// It is important that you add this route before the `Default` one.
// Routes are processed in the order they are listed,// and we need the new route to take precedence over the default.

routes.MapRoute(
    name: "Student",url: "Student/{date}",defaults: new { controller = "Home",action = "Student",date = UrlParameter.Optional}
    );

Student 操作声明:

public ActionResult Student(string date)
{
    //string dateFormat = "MM-dd-yyyy";
    string dateFormat = "dd-MM-yyyy";

    if (string.IsNullOrEmpty(date))
    {            
        return RedirectToAction("Student",new { date = DateTime.Now.ToString(dateFormat) });
    }
    else if (!DateTime.TryParseExact(date,dateFormat,CultureInfo.InvariantCulture,DateTimeStyles.None,out DateTime dt))
    {
        return RedirectToAction("ReportErrorFormat");
    }
    return View((object)date);
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...