【问题标题】:How can I find the url of the current Action in an Area from a ChildAction in MVC3?如何从 MVC3 中的 ChildAction 中找到区域中当前 Action 的 url?
【发布时间】:2012-07-19 09:34:24
【问题描述】:

我正在尝试从 asp.net mvc 中的子操作的上下文中锻炼当前路由的 url。

为此,我正在使用:

Url.RouteUrl(ControllerContext.ParentActionViewContext.RouteData.Values)

这很好,预计它不会考虑区域。如何扩展它以考虑当前区域路由?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


    【解决方案1】:

    好的,这就是我最后所做的。本质上,我检查当前操作是否有一个区域,然后将其添加到我传递给RouteUrl 调用的字典中。

    var context = ControllerContext.ParentActionViewContext;
    
    var routeValueDict = new RouteValueDictionary();
    
    foreach (var value in context.RouteData.Values)
    {
        routeValueDict.Add(value.Key, value.Value);
    }
    
    var area = context.RouteData.DataTokens["area"];
    if (area != null)
    {
        routeValueDict.Add("area", area);
    }
    
    var url = Url.RouteUrl(routeValueDict);
    return url;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 2012-06-13
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多