/// <summary> /// Formats and rewrites the link pattern with the specified <paramref name="linkName" />. /// </summary> /// <param name="linkName">Name of the link.</param> /// <param name="parameters">The parameters.</param> /// <param name="hashString">The hash string.</param> /// <param name="qualify">If set to <c>true</c> the resulting URL will be prefixed with <see cref="ServerPrefix"/>.</param> /// <returns>The full rewritten Url, ready to use</returns> /// <exception cref="ConfigurationError">Current configuration doesn't contain a link with name specified with /// <paramref name="linkName" />.</exception> internal string FormatAndRewriteUrl(string linkName, NameValueCollection parameters, string hashString = null, bool qualify = false) { if (!this.Links.ContainsKey(linkName)) { log.ErrorFormat("The url configuration doesn't contain a url with name '{0}'", linkName); return string.Format("javascript:alert('Unresolved link: {0}')", linkName); } string linkPattern = this.Links[linkName].Value; QueryString formatValues = new QueryString { { "locale", context.Locale }, { "category", context.Category } }; if (parameters != null) formatValues.Merge(parameters); string resultUrl = this.FormatPattern(linkPattern, formatValues); formatValues.Remove("locale"); formatValues.Remove("category"); string queryString = formatValues.ToString(); if (queryString != string.Empty) { string join = resultUrl.Contains("?") ? "&" : "?"; resultUrl = string.Concat(resultUrl, join, queryString); } if (!string.IsNullOrWhiteSpace(hashString)) { string join = resultUrl.Contains("#") ? string.Empty : "#"; resultUrl = string.Concat(resultUrl, join, hashString); } if (qualify) resultUrl = string.Concat(this.ServerPrefix, resultUrl); return resultUrl; }
private static void OnApplicationRequestStart(object sender, EventArgs e) { try { HttpApplication application = (HttpApplication) sender; SageContext context = new SageContext(application.Context); if (File.Exists(context.Request.PhysicalPath)) return; QueryString query = new QueryString(HttpContext.Current.Request.Url.Query); string view = query.GetString(MetaViewDictionary.ParamNameMetaView); if (view != null && !context.ProjectConfiguration.MetaViews.ContainsKey(view)) view = null; //// \.(html|xml|xmlx|htmlx)$ //// Meta view extension gets removed from the path //// Path is rewritten with ?view=$1 string path = MetaExtensionRewriter.MetaViewExpression.Replace(context.Request.Path, delegate(Match m) { if (view == null) view = m.Groups[1].Value; return string.Empty; }); if (view != null) { query.Remove(MetaViewDictionary.ParamNameMetaView); query.Add(MetaViewDictionary.ParamNameMetaView, view); string newUri = new Uri(path + query.ToString("?"), UriKind.RelativeOrAbsolute).ToString(); log.DebugFormat("Rewriting the context path from '{0}' to '{1}'.", context.Request.Path, newUri); application.Context.RewritePath(newUri); } } catch (Exception ex) { log.ErrorFormat("Failed to rewrite path: {0}", ex.Message); } }