Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / UrlHelper.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\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
19 \r
20     public class UrlHelper {\r
21         public UrlHelper(RequestContext requestContext)\r
22             : this(requestContext, RouteTable.Routes) {\r
23         }\r
24 \r
25         public UrlHelper(RequestContext requestContext, RouteCollection routeCollection) {\r
26             if (requestContext == null) {\r
27                 throw new ArgumentNullException("requestContext");\r
28             }\r
29             if (routeCollection == null) {\r
30                 throw new ArgumentNullException("routeCollection");\r
31             }\r
32             RequestContext = requestContext;\r
33             RouteCollection = routeCollection;\r
34         }\r
35 \r
36         public RequestContext RequestContext {\r
37             get;\r
38             private set;\r
39         }\r
40 \r
41         public RouteCollection RouteCollection {\r
42             get;\r
43             private set;\r
44         }\r
45 \r
46         public string Action(string actionName) {\r
47             return GenerateUrl(null /* routeName */, actionName, null, (RouteValueDictionary)null /* routeValues */);\r
48         }\r
49 \r
50         public string Action(string actionName, object routeValues) {\r
51             return GenerateUrl(null /* routeName */, actionName, null /* controllerName */, new RouteValueDictionary(routeValues));\r
52         }\r
53 \r
54         public string Action(string actionName, RouteValueDictionary routeValues) {\r
55             return GenerateUrl(null /* routeName */, actionName, null /* controllerName */, routeValues);\r
56         }\r
57 \r
58         public string Action(string actionName, string controllerName) {\r
59             return GenerateUrl(null /* routeName */, actionName, controllerName, (RouteValueDictionary)null /* routeValues */);\r
60         }\r
61 \r
62         public string Action(string actionName, string controllerName, object routeValues) {\r
63             return GenerateUrl(null /* routeName */, actionName, controllerName, new RouteValueDictionary(routeValues));\r
64         }\r
65 \r
66         public string Action(string actionName, string controllerName, RouteValueDictionary routeValues) {\r
67             return GenerateUrl(null /* routeName */, actionName, controllerName, routeValues);\r
68         }\r
69 \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
72         }\r
73 \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
76         }\r
77 \r
78         public string Content(string contentPath) {\r
79             return Content(contentPath, RequestContext.HttpContext);\r
80         }\r
81 \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
85             }\r
86 \r
87             if (contentPath[0] == '~') {\r
88                 return PathHelpers.GenerateClientUrl(httpContext, contentPath);\r
89             }\r
90             else {\r
91                 return contentPath;\r
92             }\r
93         }\r
94 \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
104         }\r
105 \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
108         }\r
109 \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
112 \r
113             if (url != null) {\r
114                 if (!String.IsNullOrEmpty(fragment)) {\r
115                     url = url + "#" + fragment;\r
116                 }\r
117 \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
122 \r
123                     string port = String.Empty;\r
124                     string requestProtocol = requestUrl.Scheme;\r
125 \r
126                     if (String.Equals(protocol, requestProtocol, StringComparison.OrdinalIgnoreCase)) {\r
127                         port = requestUrl.IsDefaultPort ? String.Empty : (":" + Convert.ToString(requestUrl.Port, CultureInfo.InvariantCulture));\r
128                     }\r
129 \r
130                     url = protocol + Uri.SchemeDelimiter + hostName + port + url;\r
131                 }\r
132             }\r
133 \r
134             return url;\r
135         }\r
136 \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
139 \r
140             VirtualPathData vpd = routeCollection.GetVirtualPath(requestContext, routeName, mergedRouteValues);\r
141             if (vpd == null) {\r
142                 return null;\r
143             }\r
144 \r
145             string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);\r
146             return modifiedUrl;\r
147         }\r
148 \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
153         }\r
154 \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
159         }\r
160 \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
165         }\r
166 \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
171         }\r
172 \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
177         }\r
178 \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
183         }\r
184 \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
189         }\r
190     }\r
191 }\r