diff --git a/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs b/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs index e205966f78..2228104c2e 100644 --- a/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs +++ b/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Specialized; +using System.Linq; +using System.Web; using System.Web.Mvc; using Umbraco.Core; using Umbraco.Core.Models; @@ -14,8 +17,10 @@ namespace Umbraco.Web.Mvc { private IPublishedContent _publishedContent; private readonly int _pageId; + private NameValueCollection _queryStringValues; private readonly UmbracoContext _umbracoContext; private string _url; + public string Url { get @@ -24,7 +29,7 @@ namespace Umbraco.Web.Mvc if (PublishedContent == null) { - throw new InvalidOperationException("Cannot redirect, no entity was found for id " + _pageId); + throw new InvalidOperationException(string.Format("Cannot redirect, no entity was found for id {0}", _pageId)); } var result = _umbracoContext.RoutingContext.UrlProvider.GetUrl(PublishedContent.Id); @@ -34,7 +39,7 @@ namespace Umbraco.Web.Mvc return _url; } - throw new InvalidOperationException("Could not route to entity with id " + _pageId + ", the NiceUrlProvider could not generate a URL"); + throw new InvalidOperationException(string.Format("Could not route to entity with id {0}, the NiceUrlProvider could not generate a URL", _pageId)); } } @@ -61,6 +66,26 @@ namespace Umbraco.Web.Mvc { } + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + public RedirectToUmbracoPageResult(int pageId, NameValueCollection queryStringValues) + : this(pageId, queryStringValues, UmbracoContext.Current) + { + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + public RedirectToUmbracoPageResult(int pageId, string queryString) + : this(pageId, queryString, UmbracoContext.Current) + { + } + /// /// Creates a new RedirectToUmbracoResult /// @@ -70,6 +95,63 @@ namespace Umbraco.Web.Mvc { } + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + public RedirectToUmbracoPageResult(IPublishedContent publishedContent, NameValueCollection queryStringValues) + : this(publishedContent, queryStringValues, UmbracoContext.Current) + { + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + public RedirectToUmbracoPageResult(IPublishedContent publishedContent, string queryString) + : this(publishedContent, queryString, UmbracoContext.Current) + { + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + public RedirectToUmbracoPageResult(int pageId, UmbracoContext umbracoContext) + { + _pageId = pageId; + _umbracoContext = umbracoContext; + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + /// + public RedirectToUmbracoPageResult(int pageId, NameValueCollection queryStringValues, UmbracoContext umbracoContext) + { + _pageId = pageId; + _queryStringValues = queryStringValues; + _umbracoContext = umbracoContext; + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + /// + public RedirectToUmbracoPageResult(int pageId, string queryString, UmbracoContext umbracoContext) + { + _pageId = pageId; + _queryStringValues = ParseQueryString(queryString); + _umbracoContext = umbracoContext; + } + /// /// Creates a new RedirectToUmbracoResult /// @@ -82,16 +164,33 @@ namespace Umbraco.Web.Mvc _umbracoContext = umbracoContext; } - /// - /// Creates a new RedirectToUmbracoResult - /// - /// - /// - public RedirectToUmbracoPageResult(int pageId, UmbracoContext umbracoContext) - { - _pageId = pageId; - _umbracoContext = umbracoContext; - } + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + /// + public RedirectToUmbracoPageResult(IPublishedContent publishedContent, NameValueCollection queryStringValues, UmbracoContext umbracoContext) + { + _publishedContent = publishedContent; + _pageId = publishedContent.Id; + _queryStringValues = queryStringValues; + _umbracoContext = umbracoContext; + } + + /// + /// Creates a new RedirectToUmbracoResult + /// + /// + /// + /// + public RedirectToUmbracoPageResult(IPublishedContent publishedContent, string queryString, UmbracoContext umbracoContext) + { + _publishedContent = publishedContent; + _pageId = publishedContent.Id; + _queryStringValues = ParseQueryString(queryString); + _umbracoContext = umbracoContext; + } public override void ExecuteResult(ControllerContext context) { @@ -103,10 +202,26 @@ namespace Umbraco.Web.Mvc } var destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext); + + if (_queryStringValues != null && _queryStringValues.Count > 0) + { + destinationUrl = destinationUrl += "?" + string.Join("&", + _queryStringValues.AllKeys.Select(x => x + "=" + HttpUtility.UrlEncode(_queryStringValues[x]))); + } + context.Controller.TempData.Keep(); context.HttpContext.Response.Redirect(destinationUrl, endResponse: false); } + private NameValueCollection ParseQueryString(string queryString) + { + if (!string.IsNullOrEmpty(queryString)) + { + return HttpUtility.ParseQueryString(queryString); + } + + return null; + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Mvc/SurfaceController.cs b/src/Umbraco.Web/Mvc/SurfaceController.cs index 1010db48df..080d8931c3 100644 --- a/src/Umbraco.Web/Mvc/SurfaceController.cs +++ b/src/Umbraco.Web/Mvc/SurfaceController.cs @@ -5,6 +5,7 @@ using System.Web.Routing; using Umbraco.Core.Models; using Umbraco.Core; using Umbraco.Web.Security; +using System.Collections.Specialized; namespace Umbraco.Web.Mvc { @@ -56,6 +57,28 @@ namespace Umbraco.Web.Mvc return new RedirectToUmbracoPageResult(pageId, UmbracoContext); } + /// + /// Redirects to the Umbraco page with the given id and passes provided querystring + /// + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId, NameValueCollection queryStringValues) + { + return new RedirectToUmbracoPageResult(pageId, queryStringValues, UmbracoContext); + } + + /// + /// Redirects to the Umbraco page with the given id and passes provided querystring + /// + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToUmbracoPage(int pageId, string queryString) + { + return new RedirectToUmbracoPageResult(pageId, queryString, UmbracoContext); + } + /// /// Redirects to the Umbraco page with the given id /// @@ -66,6 +89,28 @@ namespace Umbraco.Web.Mvc return new RedirectToUmbracoPageResult(publishedContent, UmbracoContext); } + /// + /// Redirects to the Umbraco page with the given id and passes provided querystring + /// + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent, NameValueCollection queryStringValues) + { + return new RedirectToUmbracoPageResult(publishedContent, queryStringValues, UmbracoContext); + } + + /// + /// Redirects to the Umbraco page with the given id and passes provided querystring + /// + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToUmbracoPage(IPublishedContent publishedContent, string queryString) + { + return new RedirectToUmbracoPageResult(publishedContent, queryString, UmbracoContext); + } + /// /// Redirects to the currently rendered Umbraco page /// @@ -75,6 +120,25 @@ namespace Umbraco.Web.Mvc return new RedirectToUmbracoPageResult(CurrentPage, UmbracoContext); } + /// + /// Redirects to the currently rendered Umbraco page and passes provided querystring + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage(NameValueCollection queryStringValues) + { + return new RedirectToUmbracoPageResult(CurrentPage, queryStringValues, UmbracoContext); + } + + /// + /// Redirects to the currently rendered Umbraco page and passes provided querystring + /// + /// + /// + protected RedirectToUmbracoPageResult RedirectToCurrentUmbracoPage(string queryString) + { + return new RedirectToUmbracoPageResult(CurrentPage, queryString, UmbracoContext); + } /// /// Redirects to the currently rendered Umbraco URL ///