Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / RedirectResult.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.Diagnostics.CodeAnalysis;
4     using System.Web.Mvc.Resources;
5
6     // represents a result that performs a redirection given some URI
7     public class RedirectResult : ActionResult {
8
9         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Response.Redirect() takes its URI as a string parameter.")]
10         public RedirectResult(string url)
11             : this(url, permanent: false) {
12         }
13
14         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#", Justification = "Response.Redirect() takes its URI as a string parameter.")]
15         public RedirectResult(string url, bool permanent) {
16             if (String.IsNullOrEmpty(url)) {
17                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "url");
18             }
19
20             Permanent = permanent;
21             Url = url;
22         }
23
24         public bool Permanent {
25             get;
26             private set;
27         }
28
29         [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Response.Redirect() takes its URI as a string parameter.")]
30         public string Url {
31             get;
32             private set;
33         }
34
35         public override void ExecuteResult(ControllerContext context) {
36             if (context == null) {
37                 throw new ArgumentNullException("context");
38             }
39             if (context.IsChildAction) {
40                 throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction);
41             }
42
43             string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
44             context.Controller.TempData.Keep();
45
46             if (Permanent) {
47                 context.HttpContext.Response.RedirectPermanent(destinationUrl, endResponse: false);
48             }
49             else {
50                 context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
51             }
52         }
53
54     }
55 }