问题描述
所以我的 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);
}