Url Form Action Without ViewContext

前端 未结 2 1203
时光说笑
时光说笑 2020-12-17 06:08

Is it possible to get a URL from an action without knowing ViewContext (e.g., in a controller)? Something like this:

LinkBuilder.BuildUrlFromExpression(View         


        
2条回答
  •  失恋的感觉
    2020-12-17 06:34

    Craig, Thanks for the correct answer. It works great, and it also go me thinking. So in my drive to eliminate those refactor-resistent "magic strings" I have developed a variation on your solution:

    public static string GetUrlFor(this HttpContextBase c, Expression> action)
        where T : Controller
    {
        return RouteTable.Routes.GetVirtualPath(
            new RequestContext(c, RouteTable.Routes.GetRouteData(c)), 
            GetRouteValuesFor(action)).VirtualPath;
    }
    
    public static RouteValueDictionary GetRouteValuesFor(Expression> action) 
        where T : Controller
    {
        var methodCallExpresion = ((MethodCallExpression) action.Body);
        var controllerTypeName = methodCallExpresion.Object.Type.Name;
        var routeValues = new RouteValueDictionary(new
        {
            controller = controllerTypeName.Remove(controllerTypeName.LastIndexOf("Controller")), 
            action = methodCallExpresion.Method.Name
        });
        var methodParameters = methodCallExpresion.Method.GetParameters();
        for (var i = 0; i < methodParameters.Length; i++)
        {
            var value = Expression.Lambda(methodCallExpresion.Arguments[i]).Compile().DynamicInvoke();
            var name = methodParameters[i].Name;
            routeValues.Add(name, value);
        }
        return routeValues;
    }
    

    I know what some will say...dreaded reflection! In my particular application, I think the benefit of maintainability outweighs performance conerns. I welcome any feedback on this idea and the code.

提交回复
热议问题