Merge pull request #57 from AndreasKl/master

SPRNET-1387: ASP.NET PageHandlerFactory cannot inject dependencies in the presence of the WebForms 4.0 routing infrastructure
This commit is contained in:
Marko Lahma
2013-12-03 23:55:57 -08:00
5 changed files with 100 additions and 36 deletions

View File

@@ -18,31 +18,28 @@
#endregion
#region Imports
using System;
using System.Globalization;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Web;
using System.Web.Caching;
#if NET_4_0
using System.Web.Routing;
#endif
using System.Web.SessionState;
using System.Web.UI;
using Common.Logging;
using Spring.Core.IO;
using Spring.Core.TypeConversion;
using Spring.Core.TypeResolution;
using Spring.Expressions;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
using Spring.Reflection.Dynamic;
using Spring.Threading;
using Spring.Util;
using Spring.Web.Support;
#endregion
namespace Spring.Context.Support
{
/// <summary>
@@ -181,19 +178,51 @@ namespace Spring.Context.Support
#region IHttpHandler configuration
///<summary>
/// Configures the current IHttpHandler as specified by <see cref="Spring.Web.Support.PageHandlerFactory"/>.
///</summary>
/// <summary>
/// Configures the current IHttpHandler as specified by <see cref="Spring.Web.Support.PageHandlerFactory" />. If the
/// <see cref="Spring.Web.Support.PageHandlerFactory" /> is not executed for the current request and an instance of
/// <see cref="Page" /> is served revalidate if the instance should be configured.
/// </summary>
private void OnConfigureHandler(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HandlerConfigurationMetaData hCfg = (HandlerConfigurationMetaData)LogicalThreadContext.GetData(CURRENTHANDLER_OBJECTDEFINITION);
if (hCfg != null)
{
HttpApplication app = (HttpApplication)sender;
// app.Context.Handler = // TODO: check, if this makes sense (EE)
ConfigureHandlerNow(app.Context.Handler, hCfg.ApplicationContext, hCfg.ObjectDefinitionName, hCfg.IsContainerManaged);
}
#if NET_4_0
else
{
Page page = app.Context.Handler as Page;
if (!IsPageWithRouteHandler(page))
{
return;
}
// In case of Routing pages are not handled by the PageHandlerFactory therefore no HandlerConfigurationMetaData
// is set.
IConfigurableApplicationContext applicationContext = (IConfigurableApplicationContext)WebApplicationContext.Current;
string normalizedVirtualPath = WebUtils.GetNormalizedVirtualPath(page.AppRelativeVirtualPath);
ControlInterceptor.EnsureControlIntercepted(applicationContext, page);
ConfigureHandlerNow(page, applicationContext, normalizedVirtualPath, true);
}
#endif
}
#if NET_4_0
/// <summary>
/// Determines whether the specified page is processed by a <see cref="PageRouteHandler" />.
/// </summary>
/// <param name="page">the page.</param>
/// <returns>whether the page has a page route assigned</returns>
private static bool IsPageWithRouteHandler(Page page)
{
return page != null && page.RouteData != null && page.RouteData.RouteHandler != null;
}
#endif
/// <summary>
/// Configures the specified handler instance using the object definition <paramref name="name"/>.
@@ -292,7 +321,7 @@ namespace Spring.Context.Support
}
else
{
// this is an async session timout - log as fatal since this is the thread's exit point!
// this is an async session timeout - log as fatal since this is the thread's exit point!
s_log.Fatal(msg, ex);
}
}

View File

@@ -73,12 +73,6 @@ namespace Spring.Objects.Factory.Support
s_log.Debug( "creating page instance '" + pageUrl + "'" );
}
// HttpContext ctx = HttpContext.Current;
// if (ctx == null)
// {
// throw new ObjectCreationException(
// "Unable to instantiate page. HttpContext is not defined." );
// }
IHttpHandler page;
try
{
@@ -118,23 +112,7 @@ namespace Spring.Objects.Factory.Support
/// <returns></returns>
internal static IHttpHandler CreateHandler( string pageUrl )
{
IHttpHandler page;
// HttpContext ctx = HttpContext.Current;
//#if NET_1_1
// string physicalPath = ctx.Server.MapPath(pageUrl);
// s_log.Debug(string.Format("constructing page virtual path '{0}' from physical file '{1}'", pageUrl, physicalPath));
// page = PageParser.GetCompiledPageInstance(pageUrl, physicalPath, ctx);
//#else
// string rootedVPath = WebUtils.CombineVirtualPaths( ctx.Request.CurrentExecutionFilePath, pageUrl );
// if (s_log.IsDebugEnabled)
// {
// s_log.Debug( "page vpath is " + rootedVPath );
// }
//
// page = BuildManager.CreateInstanceFromVirtualPath( rootedVPath, typeof( IHttpHandler ) ) as IHttpHandler;
//#endif
page = VirtualEnvironment.CreateInstanceFromVirtualPath(pageUrl, typeof (IHttpHandler)) as IHttpHandler;
return page;
return VirtualEnvironment.CreateInstanceFromVirtualPath(pageUrl, typeof(IHttpHandler)) as IHttpHandler;
}
/// <summary>

View File

@@ -226,6 +226,33 @@ namespace Spring.Util
}
/// <summary>
/// Gets a normalized application-relative virtual path of the given virtual path.
/// </summary>
/// <example>
/// <p>
/// Examples of what would be returned from this method given a virtual path would be:
/// </p>
/// <p>
/// <list type="bullet">
/// <item><description>'Login.aspx' => 'Login.aspx'</description></item>
/// <item><description>'~/Login.aspx' => '/Login.aspx'</description></item>
/// <item><description>'~/B2B/SignUp.aspx' => '/B2B/SignUp.aspx'</description></item>
/// <item><description>'B2B/Foo/FooServices.aspx' => 'B2B/Foo/FooServices.aspx'</description></item>
/// </list>
/// </p>
/// </example>
/// <param name="virtualPath">the virtual path.</param>
/// <returns>the normalized virtual path</returns>
public static string GetNormalizedVirtualPath(string virtualPath)
{
if(String.IsNullOrEmpty(virtualPath))
{
return virtualPath;
}
return virtualPath.StartsWith("~/") ? virtualPath.Substring(1) : virtualPath;
}
/// <summary>
/// Gets the virtual path portion of the given absolute URL
/// relative to the given base path.
/// </summary>

View File

@@ -1,4 +1,24 @@
using Apache.NMS;
#region License
/*
* Copyright © 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using Apache.NMS;
using NUnit.Framework;
using Spring.Collections;

View File

@@ -271,5 +271,15 @@ namespace Spring.Util
Assert.AreEqual("/mYpath", WebUtils.GetRelativePath("/Mydir", "/mYdir/mYpath"));
Assert.AreEqual("/myotherdir/mypath", WebUtils.GetRelativePath("/mydir", "/myotherdir/mypath"));
}
[Test]
public void GetNormalizedVirtualPath()
{
Assert.AreEqual(null, WebUtils.GetNormalizedVirtualPath(null));
Assert.AreEqual(String.Empty, WebUtils.GetNormalizedVirtualPath(String.Empty));
Assert.AreEqual("~test.aspx", WebUtils.GetNormalizedVirtualPath("~test.aspx"));
Assert.AreEqual("/test.aspx", WebUtils.GetNormalizedVirtualPath("~/test.aspx"));
Assert.AreEqual("/Complex.Path/~/test.aspx", WebUtils.GetNormalizedVirtualPath("~/Complex.Path/~/test.aspx"));
}
}
}