是否可以跨 ASP.NET MVC 框架重写 UrlHelper.GenerateUrl() ?

发布于 2024-12-21 03:38:40 字数 1843 浏览 1 评论 0原文

方法 UrlHelper.GenerateUrl() 位于 System.Web.Mvc.UrlHelper

我需要一个不同的实现,它将影响从 Url.Action() 到 Html.BeginForm() 的所有内容。

这是当前的 .NET 实现:

    [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; 
    } 

我知道可以使用委托重写静态方法,但这不允许从不同类对同一方法的所有方法调用都使用我的实现而不是默认的实现。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-12-28 03:38:40

你所要求的是不可能的。如果您想修改 Url 和 Html 帮助程序的输出,您可以编写自定义帮助程序或简单地更改路由定义。帮助程序在生成 url 时使用这些路由定义。如果您对整个应用程序中的 url 的外观有一些非常具体的要求,您甚至可以编写自定义路由。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文