/// <summary> /// Permanent Redirect to given action or url. Status code : 301 /// </summary> /// <param name="context"></param> /// <param name="routeValueDictionary"></param> public static void RedirectPermanentTo(this ActionExecutingContext context, RouteValueDictionary routeValueDictionary) { var httpContext = context.HttpContext; if (!context.IsRedirecting()) { httpContext.Response.StatusCode = 301; httpContext.Response.Status = "301 Moved Permanently"; RedirectTo(context, routeValueDictionary); } }
/// <summary> /// Redirect to given action or url. /// </summary> /// <param name="context"></param> /// <param name="routeValueDictionary"></param> public static void RedirectTo(this ActionExecutingContext context, RouteValueDictionary routeValueDictionary) { var httpContext = context.HttpContext; if (!context.IsRedirecting()) { context.Result = new RedirectToRouteResult( new RouteValueDictionary(routeValueDictionary) ); context.Result.ExecuteResult(context.Controller.ControllerContext); } }
/// <summary> /// Permanent Redirect to given action or url only if the action is not same as this. /// If any redirect is happing then it will not execute. /// Note : This will resolve the looping of redirection. /// </summary> /// <param name="context"></param> /// <param name="action"></param> /// <param name="controller">If not passed then current controller will be given.</param> /// <param name="area">If not passed then current area will be given.</param> public static void RedirectToActionPermanentIfDistinct(this ActionExecutingContext context, string action, string controller = null, string area = null) { if (!context.IsRedirecting()) { controller = string.IsNullOrEmpty(controller) ? GetControllerName(context) : controller; area = string.IsNullOrEmpty(area) ? GetAreaName(context) : area; var isCurrentUrlIsSameAs = IsCurrentUrl(context, action, controller, area); if (isCurrentUrlIsSameAs == false) { RedirectPermanentTo(context, action, controller, area); } } }
/// <summary> /// Permanent Redirect to given action or url. Status code : 301 /// </summary> /// <param name="context"></param> /// <param name="action"></param> /// <param name="controller">If not passed then current controller will be given.</param> /// <param name="area">If not passed then current area will be given.</param> public static void RedirectPermanentTo(this ActionExecutingContext context, string action, string controller, string area) { var httpContext = context.HttpContext; if (!context.IsRedirecting()) { if (!httpContext.Response.IsRequestBeingRedirected) { httpContext.Response.StatusCode = 301; httpContext.Response.Status = "301 Moved Permanently"; RedirectTo(context, new RouteValueDictionary(new { controller, action, area })); } } }