微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用“取消”按钮时的ASP.NET MVC路由

如何解决使用“取消”按钮时的ASP.NET MVC路由

我正在整个Web应用程序中使用ParentChild路由模式,该模式将具有类似此类的Urls。

使用ParentChild MapRoute可以大大简化整个应用程序中的控制器方法

https://localhost:44307/ProjectTestEvents/549084af-0e7a-4d2d-a709-04c599ca778e/Details/895d72c1-f667-49d8-b487-5705931b82c9

RouteConfig.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将导致具有querystring参数的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结构。

任何建议表示赞赏

解决方法

为什么你不能改变

url: "{controller}/{parentid}/{action}/{childid}"

url: "{controller}/{action}/{parentid}/{childid}

或添加路线属性

[Route("~/ProjectTestEvents/Details/{parentid}/{childid}
public ActionResult Details(GenericParentChildModel ParentChild)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。