1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Diagnostics.CodeAnalysis;
\r
16 using System.Globalization;
\r
17 using System.Web.Mvc.Resources;
\r
18 using System.Web.Routing;
\r
20 public class UrlHelper {
\r
21 public UrlHelper(RequestContext requestContext)
\r
22 : this(requestContext, RouteTable.Routes) {
\r
25 public UrlHelper(RequestContext requestContext, RouteCollection routeCollection) {
\r
26 if (requestContext == null) {
\r
27 throw new ArgumentNullException("requestContext");
\r
29 if (routeCollection == null) {
\r
30 throw new ArgumentNullException("routeCollection");
\r
32 RequestContext = requestContext;
\r
33 RouteCollection = routeCollection;
\r
36 public RequestContext RequestContext {
\r
41 public RouteCollection RouteCollection {
\r
46 public string Action(string actionName) {
\r
47 return GenerateUrl(null /* routeName */, actionName, null, (RouteValueDictionary)null /* routeValues */);
\r
50 public string Action(string actionName, object routeValues) {
\r
51 return GenerateUrl(null /* routeName */, actionName, null /* controllerName */, new RouteValueDictionary(routeValues));
\r
54 public string Action(string actionName, RouteValueDictionary routeValues) {
\r
55 return GenerateUrl(null /* routeName */, actionName, null /* controllerName */, routeValues);
\r
58 public string Action(string actionName, string controllerName) {
\r
59 return GenerateUrl(null /* routeName */, actionName, controllerName, (RouteValueDictionary)null /* routeValues */);
\r
62 public string Action(string actionName, string controllerName, object routeValues) {
\r
63 return GenerateUrl(null /* routeName */, actionName, controllerName, new RouteValueDictionary(routeValues));
\r
66 public string Action(string actionName, string controllerName, RouteValueDictionary routeValues) {
\r
67 return GenerateUrl(null /* routeName */, actionName, controllerName, routeValues);
\r
70 public string Action(string actionName, string controllerName, object routeValues, string protocol) {
\r
71 return GenerateUrl(null /* routeName */, actionName, controllerName, protocol, null /* hostName */, null /* fragment */, new RouteValueDictionary(routeValues), RouteCollection, RequestContext, true /* includeImplicitMvcValues */);
\r
74 public string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName) {
\r
75 return GenerateUrl(null /* routeName */, actionName, controllerName, protocol, hostName, null /* fragment */, routeValues, RouteCollection, RequestContext, true /* includeImplicitMvcValues */);
\r
78 public string Content(string contentPath) {
\r
79 return Content(contentPath, RequestContext.HttpContext);
\r
82 internal static string Content(string contentPath, HttpContextBase httpContext) {
\r
83 if (String.IsNullOrEmpty(contentPath)) {
\r
84 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "contentPath");
\r
87 if (contentPath[0] == '~') {
\r
88 return PathHelpers.GenerateClientUrl(httpContext, contentPath);
\r
95 //REVIEW: Should we have an overload that takes Uri?
\r
96 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
97 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
98 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
\r
99 Justification = "Needs to take same parameters as HttpUtility.UrlEncode()")]
\r
100 [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
\r
101 Justification = "For consistency, all helpers are instance methods.")]
\r
102 public string Encode(string url) {
\r
103 return HttpUtility.UrlEncode(url);
\r
106 private string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues) {
\r
107 return GenerateUrl(routeName, actionName, controllerName, routeValues, RouteCollection, RequestContext, true /* includeImplicitMvcValues */);
\r
110 internal static string GenerateUrl(string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) {
\r
111 string url = GenerateUrl(routeName, actionName, controllerName, routeValues, routeCollection, requestContext, includeImplicitMvcValues);
\r
114 if (!String.IsNullOrEmpty(fragment)) {
\r
115 url = url + "#" + fragment;
\r
118 if (!String.IsNullOrEmpty(protocol) || !String.IsNullOrEmpty(hostName)) {
\r
119 Uri requestUrl = requestContext.HttpContext.Request.Url;
\r
120 protocol = (!String.IsNullOrEmpty(protocol)) ? protocol : Uri.UriSchemeHttp;
\r
121 hostName = (!String.IsNullOrEmpty(hostName)) ? hostName : requestUrl.Host;
\r
123 string port = String.Empty;
\r
124 string requestProtocol = requestUrl.Scheme;
\r
126 if (String.Equals(protocol, requestProtocol, StringComparison.OrdinalIgnoreCase)) {
\r
127 port = requestUrl.IsDefaultPort ? String.Empty : (":" + Convert.ToString(requestUrl.Port, CultureInfo.InvariantCulture));
\r
130 url = protocol + Uri.SchemeDelimiter + hostName + port + url;
\r
137 internal static string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) {
\r
138 RouteValueDictionary mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, requestContext.RouteData.Values, routeValues, includeImplicitMvcValues);
\r
140 VirtualPathData vpd = routeCollection.GetVirtualPath(requestContext, routeName, mergedRouteValues);
\r
145 string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
\r
146 return modifiedUrl;
\r
149 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
150 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
151 public string RouteUrl(object routeValues) {
\r
152 return RouteUrl(null /* routeName */, routeValues);
\r
155 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
156 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
157 public string RouteUrl(RouteValueDictionary routeValues) {
\r
158 return RouteUrl(null /* routeName */, routeValues);
\r
161 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
162 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
163 public string RouteUrl(string routeName) {
\r
164 return RouteUrl(routeName, (object)null /* routeValues */);
\r
167 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
168 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
169 public string RouteUrl(string routeName, object routeValues) {
\r
170 return RouteUrl(routeName, routeValues, null /* protocol */);
\r
173 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
174 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
175 public string RouteUrl(string routeName, RouteValueDictionary routeValues) {
\r
176 return RouteUrl(routeName, routeValues, null /* protocol */, null /* hostName */);
\r
179 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
180 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
181 public string RouteUrl(string routeName, object routeValues, string protocol) {
\r
182 return GenerateUrl(routeName, null /* actionName */, null /* controllerName */, protocol, null /* hostName */, null /* fragment */, new RouteValueDictionary(routeValues), RouteCollection, RequestContext, false /* includeImplicitMvcValues */);
\r
185 [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings",
\r
186 Justification = "As the return value will used only for rendering, string return value is more appropriate.")]
\r
187 public string RouteUrl(string routeName, RouteValueDictionary routeValues, string protocol, string hostName) {
\r
188 return GenerateUrl(routeName, null /* actionName */, null /* controllerName */, protocol, hostName, null /* fragment */, routeValues, RouteCollection, RequestContext, false /* includeImplicitMvcValues */);
\r