public override void OnActionExecuting( ActionExecutingContext Context) { HttpRequestBase Request = Context.HttpContext.Request; HttpResponseBase Response = Context.HttpContext.Response; if (!Request.IsLocal) { if (!Request.IsSecureConnection && this.ForceHttps) // http://domain.com OR http://www.domain.com { if (this.ForceWww && !Request.Url.Host.Contains("www.")) // http://domain.com { Response.RedirectPermanent(new Uri(Uri.UriSchemeHttps + "://www." + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true); } else // http://www.domain.com { Response.RedirectPermanent(new Uri(Uri.UriSchemeHttps + "://" + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true); }; } else { if (this.ForceWww && !Request.Url.Host.Contains("www.")) // http://domain.com OR https://domain.com { Response.RedirectPermanent(new Uri(Request.Url.Scheme + "://www." + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true); } ; }; } ; }
/// <summary> /// Attempt to determine the file path to the requested markdown file /// the path could be part of the same Mater directory or could be a /// virtual directory mapped in IIS or Azure /// </summary> /// <param name="Response">ASP.NET Response object.</param> /// <param name="path">Web path used for finding the requested md file</param> /// <returns></returns> string GetMarkdownPath(HttpResponseBase Response, string path) { // Root, e.g. "/" if (null == path) { return(Server.MapPath("~/articles/index.md")); } // Directory, e.g. "/home/" attempts to find an index.md file if (path.EndsWith("/", StringComparison.InvariantCulture)) { return(Server.MapPath("~/articles/" + path + "index.md")); } // Directory or file, e.g. "/home" if (System.IO.File.Exists(Server.MapPath("~/articles/" + path + "/index.md"))) { // 301 Response.RedirectPermanent("/" + path + "/", true); } else if (System.IO.File.Exists(Server.MapPath("~/articles/" + path + ".md"))) { return(Server.MapPath("~/articles/" + path + ".md")); } throw new System.IO.FileNotFoundException("No directories or files found for " + path); }
/// <summary> /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class. /// </summary> /// <param name="context">The context within which the result is executed.</param><exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception> public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (context.IsChildAction) { throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction); } string str = UrlHelper.GenerateUrl(this.RouteName, (string)null, (string)null, this.RouteValues, this.Routes, context.RequestContext, false); if (string.IsNullOrEmpty(str)) { throw new InvalidOperationException(MvcResources.Common_NoRouteMatched); } context.Controller.TempData.Keep(); if (this.Permanent) { HttpResponseBase response = context.HttpContext.Response; bool flag = false; string url = str; int num = flag ? 1 : 0; response.RedirectPermanent(url, num != 0); } else { HttpResponseBase response = context.HttpContext.Response; bool flag = false; string url = str; int num = flag ? 1 : 0; response.Redirect(url, num != 0); } }
//TODO: This should only be called in one place total. And it needs to be tested. public static Entry GetEntryFromRequest(bool allowRedirectToEntryName, ISubtextContext context) { string slug = context.RequestContext.GetSlugFromRequest(); if (!String.IsNullOrEmpty(slug)) { return(GetEntry(slug, context)); } int?id = context.RequestContext.GetIdFromRequest(); if (id != null) { Entry entry = GetEntry(id.Value, context); if (entry == null) { return(null); } //TODO: Violation of SRP here! //Second condition avoids infinite redirect loop. Should never happen. if (allowRedirectToEntryName && entry.HasEntryName && !entry.EntryName.IsNumeric()) { HttpResponseBase response = context.HttpContext.Response; response.RedirectPermanent(context.UrlHelper.EntryUrl(entry).ToFullyQualifiedUrl(context.Blog).ToString()); } return(entry); } return(null); }
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } string url = UrlHelper.GenerateContentUrl(_url, context.HttpContext); context.Controller.TempData.Keep(); HttpResponseBase response = context.HttpContext.Response; response.RedirectPermanent(url, endResponse: true); }
/// <summary> /// /// </summary> /// <param name="response"></param> /// <param name="location"></param> public static void RedirectPermanentWithoutEncodingURL(this HttpResponseBase response, string location) { if (HttpContext.Current.Request.Browser.Browser.ToLower().Equals("ie") && (HttpContext.Current.Request.Browser.MajorVersion == 6)) { response.RedirectPermanent(location); } else { response.Clear(); response.StatusCode = 301; response.Status = "301 Moved Permanently"; response.AddHeader("Location", location); response.End(); } }
/// <summary> /// Fixes the request Url, if required. Returns a value indicating whether a redirect is necessary. /// </summary> public bool ValidateUrl() { HttpRequestBase request = context.Request; HttpResponseBase response = context.Response; // load balancers in production trick us into using the wrong port, we won't fall for that. Uri requestUrl = request.Url.WithPublicPort(); string incoming = requestUrl.GetLeftPart(UriPartial.Path); string absolute = requestUrl.AbsolutePath; string decoded = HttpUtility.UrlDecode(absolute); string redirect; if (absolute != decoded) { // encoded characters such as %7D result in issues, so we better leave them untouched. string host = requestUrl.GetLeftPart(UriPartial.Authority); redirect = host.ToLowerInvariant() + absolute; } else { redirect = incoming.ToLowerInvariant(); // our urls are always in lower case. } // remove www. from the beginning of the url. redirect = CompiledRegex.WwwSubdomain.Replace(redirect, Resources.Shared.Regex.WwwSubdomainReplacement); if (absolute.Length > 1 && absolute.EndsWith("/")) { // remove '/' from the end of the url unless this is the only character in the url. redirect = redirect.Substring(0, redirect.Length - 1); } if (redirect != incoming) { // append the query string to the redirect result. NameValueCollection query = request.QueryString; string queryString = string.Empty; if (query.Count > 0) { queryString = "?{0}".FormatWith(query.ToString()); } response.RedirectPermanent(redirect + queryString); return(true); } return(false); }
public override void RedirectPermanent(string url) { _response.RedirectPermanent(url); }