Ambient values in mvc2.net routing

前端 未结 6 1925
生来不讨喜
生来不讨喜 2020-12-16 06:41

I have following two routes registered in my global.asax file

routes.MapRoute(
    \"strict\",
    \"{controller}.mvc/{docid}/{action}/{id}\",
          


        
6条回答
  •  悲哀的现实
    2020-12-16 07:34

    Muhammad, I suggest something like this : (written 5 mn ago, not tested in production)

    public static class MyHtmlHelperExtensions {
    
        public static MvcHtmlString FixActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes) {
            var linkRvd = new RouteValueDictionary(routeValues);
            var contextRvd = htmlHelper.ViewContext.RouteData.Values;
            var contextRemovedRvd = new RouteValueDictionary();
    
            // remove clearing route values from current context
            foreach (var rv in linkRvd) {
                if (string.IsNullOrEmpty((string)rv.Value) && contextRvd.ContainsKey(rv.Key)) {
                    contextRemovedRvd.Add(rv.Key, contextRvd[rv.Key]);
                    contextRvd.Remove(rv.Key);
                }
            }
    
            // call ActionLink with modified context
            var htmlString = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
    
            // restore context
            foreach (var rv in contextRemovedRvd) {
                contextRvd.Add(rv.Key, rv.Value);
            }
    
            return htmlString;
        }
    }
    

提交回复
热议问题