我在整个 Web 应用程序中使用 ParentChild 路由模式,该模式将具有这样的 Url。
使用 ParentChild MapRoute 极大地简化了整个应用程序中的控制器方法。
https://localhost:44307/ProjectTestEvents/549084af-0e7a-4d2d-a709-04c599ca778e/Details/895d72c1-f667-49d8-b487-5705931b82c9
路由配置.cs
routes.MapRoute(
name: "ParentChild",
url: "{controller}/{parentid}/{action}/{childid}",
defaults: new { controller = "Login", action = "Index",
parentid = UrlParameter.Optional,
childid = UrlParameter.Optional
}
);
控制器方法:
public ActionResult Details(GenericParentChildModel ParentChild)
{
// do some stuff ...
}
型号类:
public class GenericParentChildModel
{
public Guid ParentId { get; set; }
public string ParentDescription { get; set; }
public Guid ChildId {get; set; }
public string ChildDescription { get; set; }
}
当用户选择表单的取消按钮并且导航被重定向到上一点时,我想应用相同的方法。
在表单上,我有一个取消按钮,它调用包含 RedirectToAction 的通用 Return 方法。
public ActionResult Return(GenericParentChildModel ParentChild)
{
return RedirectToAction("Details", new RouteValueDictionary(
new { controller = "ProjectTestEvents", action = "Details",
parentid = ParentChild.ParentId,
childid = ParentChild.ChildId }));
}
Url 将产生一个带有查询字符串参数的 Url ...
https://localhost:44307/ProjectTestEvents/Details?parentid=549084af-0e7a-4d2d-a709-04c599ca778e&childid=76bd0fd7-fd4f-4ff1-b357-8a2acc8c6ff7
而不是所需的 ParentChild 格式。
https://localhost:44307/ProjectTestEvents/549084af-0e7a-4d2d-a709-04c599ca778e/Details/895d72c1-f667-49d8-b487-5705931b82c9
查询字符串版本有效,但我宁愿保留用户最初访问该路径时使用的 url 结构。
任何建议表示赞赏