是否可以跨 ASP.NET MVC 框架重写 UrlHelper.GenerateUrl() ?
方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所要求的是不可能的。如果您想修改 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.