Pregunta

The method UrlHelper.GenerateUrl() is located in System.Web.Mvc.UrlHelper

I need a different implementation that would affect everything from Url.Action() to Html.BeginForm().

This is the current .NET implementation:

    [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
    public static string GenerateUrl(string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) { 
        string url = GenerateUrl(routeName, actionName, controllerName, routeValues, routeCollection, requestContext, includeImplicitMvcValues);

        if (url != null) {
            if (!String.IsNullOrEmpty(fragment)) { 
                url = url + "#" + fragment;
            } 

            if (!String.IsNullOrEmpty(protocol) || !String.IsNullOrEmpty(hostName)) {
                Uri requestUrl = requestContext.HttpContext.Request.Url; 
                protocol = (!String.IsNullOrEmpty(protocol)) ? protocol : Uri.UriSchemeHttp;
                hostName = (!String.IsNullOrEmpty(hostName)) ? hostName : requestUrl.Host;

                string port = String.Empty; 
                string requestProtocol = requestUrl.Scheme;

                if (String.Equals(protocol, requestProtocol, StringComparison.OrdinalIgnoreCase)) { 
                    port = requestUrl.IsDefaultPort ? String.Empty : (":" + Convert.ToString(requestUrl.Port, CultureInfo.InvariantCulture));
                } 

                url = protocol + Uri.SchemeDelimiter + hostName + port + url;
            }
        } 

        return url; 
    } 

I know that its possible to override static methods using delegates, but that won't allow all of method calls to the same method from different classes use my implementation instead of the default one.

¿Fue útil?

Solución

What you are asking for is not possible. If you want to modify the output of Url and Html helpers you could either write custom ones or simply change your route definitions. Helpers use those route definitions when generating urls. You could even write custom routes if you have some very specific requirements about how the urls should look like throughout your entire application.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top