Jelajahi Sumber

2010-05-06 Marek Habersack <[email protected]>

	* HttpResponse.cs: implemented the following 4.0 methods:
	RedirectPermanent, RedirectToRoute, RedirectToRoutePermanent and
	RemoveOutputCacheItem.

2010-05-06  Marek Habersack  <[email protected]>

	* RouteCollection.cs: GetVirtualPath throws ArgumentException
	when named route is not found in the collection.

2010-05-06  Marek Habersack  <[email protected]>

	* OutputCacheModule.cs: added a 2.0 internal property to return
	the internal provider used in this profile.

	* OutputCache.cs: added internal RemoveFromProvider method to
	remove items from the indicated provider.

svn path=/trunk/mcs/; revision=156933
Marek Habersack 15 tahun lalu
induk
melakukan
7d005d6a34

+ 5 - 0
mcs/class/System.Web.Routing/System.Web.Routing/ChangeLog

@@ -1,3 +1,8 @@
+2010-05-06  Marek Habersack  <[email protected]>
+
+	* RouteCollection.cs: GetVirtualPath throws ArgumentException
+	when named route is not found in the collection.
+
 2010-05-05  Marek Habersack  <[email protected]>
 
 	* UrlRoutingModule.cs: PostMapRequestHandler is obsolete in 4.0

+ 6 - 2
mcs/class/System.Web.Routing/System.Web.Routing/RouteCollection.cs

@@ -155,15 +155,19 @@ namespace System.Web.Routing
 		{
 			if (requestContext == null)
 				throw new ArgumentNullException ("httpContext");
-
+#if !NET_4_0
 			if (Count == 0)
 				return null;
-
+#endif
 			VirtualPathData vp = null;
 			if (!String.IsNullOrEmpty (name)) {
 				RouteBase rb = this [name];
 				if (rb != null)
 					vp = rb.GetVirtualPath (requestContext, values);
+#if NET_4_0
+				else
+					throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
+#endif
 			} else {
 				foreach (RouteBase rb in this) {
 					vp = rb.GetVirtualPath (requestContext, values);

+ 8 - 0
mcs/class/System.Web/System.Web.Caching/ChangeLog

@@ -1,3 +1,11 @@
+2010-05-06  Marek Habersack  <[email protected]>
+
+	* OutputCacheModule.cs: added a 2.0 internal property to return
+	the internal provider used in this profile.
+
+	* OutputCache.cs: added internal RemoveFromProvider method to
+	remove items from the indicated provider.
+
 2010-02-23  Marek Habersack  <[email protected]>
 
 	* CachedVaryBy.cs: made serializable for 4.0+ (necessary for

+ 21 - 1
mcs/class/System.Web/System.Web.Caching/OutputCache.cs

@@ -27,6 +27,7 @@
 //
 
 using System.Configuration;
+using System.Configuration.Provider;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Security.Permissions;
@@ -93,7 +94,7 @@ namespace System.Web.Caching
 				!(data is FileResponseElement) &&
 				!(data is SubstitutionResponseElement);
 		}
-
+		
 		static void Init ()
 		{
 			if (initialized)
@@ -132,5 +133,24 @@ namespace System.Web.Caching
 
 			return ret;
 		}
+
+		internal static void RemoveFromProvider (string key, string providerName)
+		{
+			if (providerName == null)
+				return;
+
+			OutputCacheProviderCollection providers = Providers;
+			OutputCacheProvider provider;
+			
+			if (providers == null || providers.Count == 0)
+				provider = null;
+			else
+				provider = providers [providerName];
+
+			if (provider == null)
+				throw new ProviderException ("Provider '" + providerName + "' was not found.");
+
+			provider.Remove (key);
+		}
 	}
 }

+ 5 - 1
mcs/class/System.Web/System.Web.Caching/OutputCacheModule.cs

@@ -50,7 +50,11 @@ namespace System.Web.Caching
 		static object keysCacheLock = new object ();
 		Dictionary <string, string> keysCache;
 		Dictionary <string, string> entriesToInvalidate;
-
+#if !NET_4_0
+		internal OutputCacheProvider InternalProvider {
+			get { return provider; }
+		}
+#endif
 		public OutputCacheModule ()
 		{
 		}

+ 6 - 0
mcs/class/System.Web/System.Web/ChangeLog

@@ -1,3 +1,9 @@
+2010-05-06  Marek Habersack  <[email protected]>
+
+	* HttpResponse.cs: implemented the following 4.0 methods:
+	RedirectPermanent, RedirectToRoute, RedirectToRoutePermanent and
+	RemoveOutputCacheItem.
+
 2010-05-05  Marek Habersack  <[email protected]>
 
 	* HttpRequest.cs: implemented new 4.0 property - RequestContext

+ 117 - 8
mcs/class/System.Web/System.Web/HttpResponse.cs

@@ -43,6 +43,10 @@ using System.Security.Permissions;
 using System.Web.Hosting;
 using System.Web.SessionState;
 
+#if NET_4_0
+using System.Web.Routing;
+#endif
+
 namespace System.Web
 {	
 	// CAS - no InheritanceDemand here as the class is sealed
@@ -832,12 +836,7 @@ namespace System.Web
 			AppendHeader ("PICS-Label", value);
 		}
 
-		public void Redirect (string url)
-		{
-			Redirect (url, true);
-		}
-
-		public void Redirect (string url, bool endResponse)
+		void Redirect (string url, bool endResponse, int code)
 		{
 			if (url == null)
 				throw new ArgumentNullException ("url");
@@ -852,7 +851,7 @@ namespace System.Web
 			ClearHeaders ();
 			ClearContent ();
 			
-			StatusCode = 302;
+			StatusCode = code;
 			url = ApplyAppPathModifier (url);
 
 			bool isFullyQualified;
@@ -888,7 +887,105 @@ namespace System.Web
 				End ();
 			is_request_being_redirected = false;
 		}
+		
+		public void Redirect (string url)
+		{
+			Redirect (url, true);
+		}
+
+		public void Redirect (string url, bool endResponse)
+		{
+			Redirect (url, endResponse, 302);
+		}
+#if NET_4_0
+		public void RedirectPermanent (string url)
+		{
+			RedirectPermanent (url, true);
+		}
+
+		public void RedirectPermanent (string url, bool endResponse)
+		{
+			Redirect (url, endResponse, 301);
+		}
+
+		public void RedirectToRoute (object routeValues)
+		{
+			RedirectToRoute ("RedirectToRoute", null, new RouteValueDictionary (routeValues), 302, true);
+		}
+
+		public void RedirectToRoute (RouteValueDictionary routeValues)
+		{
+			RedirectToRoute ("RedirectToRoute", null, routeValues, 302, true);
+		}
+
+		public void RedirectToRoute (string routeName)
+		{
+			RedirectToRoute ("RedirectToRoute", routeName, null, 302, true);
+		}
+
+		public void RedirectToRoute (string routeName, object routeValues)
+		{
+			RedirectToRoute ("RedirectToRoute", routeName, new RouteValueDictionary (routeValues), 302, true);
+		}
+
+		public void RedirectToRoute (string routeName, RouteValueDictionary routeValues)
+		{
+			RedirectToRoute ("RedirectToRoute", routeName, routeValues, 302, true);
+		}
+
+		public void RedirectToRoutePermanent (object routeValues)
+		{
+			RedirectToRoute ("RedirectToRoutePermanent", null, new RouteValueDictionary (routeValues), 301, false);
+		}
+
+		public void RedirectToRoutePermanent (RouteValueDictionary routeValues)
+		{
+			RedirectToRoute ("RedirectToRoutePermanent", null, routeValues, 301, false);
+		}
+
+		public void RedirectToRoutePermanent (string routeName)
+		{
+			RedirectToRoute ("RedirectToRoutePermanent", routeName, null, 301, false);
+		}
+
+		public void RedirectToRoutePermanent (string routeName, object routeValues)
+		{
+			RedirectToRoute ("RedirectToRoutePermanent", routeName, new RouteValueDictionary (routeValues), 301, false);
+		}		
 
+		public void RedirectToRoutePermanent (string routeName, RouteValueDictionary routeValues)
+		{
+			RedirectToRoute ("RedirectToRoutePermanent", routeName, routeValues, 301, false);
+		}
+		
+		void RedirectToRoute (string callerName, string routeName, RouteValueDictionary routeValues, int redirectCode, bool endResponse)
+		{
+			HttpContext ctx = context ?? HttpContext.Current;
+			HttpRequest req = ctx != null ? ctx.Request : null;
+			
+			if (req == null)
+				// Let's emulate .NET
+				throw new NullReferenceException ();
+			
+			VirtualPathData vpd = RouteTable.Routes.GetVirtualPath (req.RequestContext, routeName, routeValues);
+			string redirectUrl = vpd != null ? vpd.VirtualPath : null;
+			if (String.IsNullOrEmpty (redirectUrl))
+				throw new InvalidOperationException ("No matching route found for RedirectToRoute");
+
+			Redirect (redirectUrl, true, redirectCode);
+		}
+
+		public static void RemoveOutputCacheItem (string path, string providerName)
+		{
+			if (path == null)
+				throw new ArgumentNullException ("path");
+
+			if (path.Length > 0 && path [0] != '/')
+				throw new ArgumentException ("Invalid path for HttpResponse.RemoveOutputCacheItem: '" + path + "'. An absolute virtual path is expected");
+
+			OutputCache.RemoveFromProvider (path, providerName);
+		}
+#endif
 		public static void RemoveOutputCacheItem (string path)
 		{
 			if (path == null)
@@ -900,7 +997,19 @@ namespace System.Web
 			if (path [0] != '/')
 				throw new ArgumentException ("'" + path + "' is not an absolute virtual path.");
 
-			HttpRuntime.InternalCache.Remove (path);
+#if NET_4_0
+			RemoveOutputCacheItem (path, OutputCache.DefaultProviderName);
+#else
+			HttpContext context = HttpContext.Current;
+			HttpApplication app_instance = context != null ? context.ApplicationInstance : null;
+			HttpModuleCollection modules = app_instance != null ? app_instance.Modules : null;
+			OutputCacheModule ocm = modules != null ? modules.Get ("OutputCache") as OutputCacheModule : null;
+			OutputCacheProvider internalProvider = ocm != null ? ocm.InternalProvider : null;
+			if (internalProvider == null)
+				return;
+
+			internalProvider.Remove (path);
+#endif
 		}
 
 		public void SetCookie (HttpCookie cookie)

+ 51 - 1
mcs/class/System.Web/Test/System.Web/HttpResponseTest.cs

@@ -32,9 +32,14 @@ using System.Collections.Specialized;
 using System.IO;
 using System.Text;
 using System.Web;
+#if NET_4_0
+using System.Web.Routing;
+#endif
 
 using NUnit.Framework;
 
+using MonoTests.Common;
+
 namespace MonoTests.System.Web {
 
 	public class FakeHttpWorkerRequest2 : HttpWorkerRequest {
@@ -61,7 +66,7 @@ namespace MonoTests.System.Web {
 		
 		public override string GetRawUrl()
 		{
-			return "GetRawUrl";
+			return "/GetRawUrl";
 		}
 		
 		public override string GetHttpVerbName()
@@ -588,6 +593,51 @@ namespace MonoTests.System.Web {
 			Assert.AreEqual (0, f.UnknownResponseHeaders.Count, "#C1");
 #endif
 		}
+#if NET_4_0
+		[Test]
+		public void RedirectPermanent ()
+		{
+			FakeHttpWorkerRequest2 request;
+			HttpContext context = Cook (1, out request);
+			AssertExtensions.Throws<ArgumentNullException> (() => {
+				context.Response.RedirectPermanent (null);
+			}, "#A1");
+
+			AssertExtensions.Throws<ArgumentException> (() => {
+				context.Response.RedirectPermanent ("http://invalid\nurl.com");
+			}, "#A2");
+
+			AssertExtensions.Throws<ArgumentNullException> (() => {
+				context.Response.RedirectPermanent (null, true);
+			}, "#A3");
+
+			AssertExtensions.Throws<ArgumentException> (() => {
+				context.Response.RedirectPermanent ("http://invalid\nurl.com", true);
+			}, "#A4");
+		}
+
+		[Test]
+		public void RedirectToRoute ()
+		{
+			var resp = new HttpResponse (new StringWriter ());
+			// Ho, ho, ho!
+			AssertExtensions.Throws<NullReferenceException> (() => {
+				resp.RedirectToRoute ("SomeRoute");
+			}, "#A1");
+
+			FakeHttpWorkerRequest2 request;
+			HttpContext context = Cook (1, out request);
+
+			// From RouteCollection.GetVirtualPath
+			AssertExtensions.Throws<ArgumentException> (() => {
+				context.Response.RedirectToRoute ("SomeRoute");
+			}, "#A2");
+
+			AssertExtensions.Throws<InvalidOperationException> (() => {
+				context.Response.RedirectToRoute (new { productId = "1", category = "widgets" });
+			}, "#A3");
+		}
+#endif
 	}
 
 	[TestFixture]