added SPRNET-711, SPRNET-713 - MappingHandlerFactory for configuring custom IHttpHandler and IHttpHandlerFactory instances

This commit is contained in:
eeichinger
2008-10-13 23:18:44 +00:00
parent 054421552e
commit b6b4b370f9
18 changed files with 1014 additions and 102 deletions

View File

@@ -0,0 +1,20 @@
using System.Web;
/// <summary>
/// Summary description for NoOpHandler
/// </summary>
public class MyCustomHttpHandler : IHttpHandler
{
public string MessageText = "<unconfigured>";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(@"Response from MyCustomHttpHandler:" + MessageText);
}
public bool IsReusable
{
get { return true; }
}
}

View File

@@ -0,0 +1,49 @@
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title><%=Title%></title>
</head>
<body>
<h2><a href="../../Default.aspx">Welcome to Spring.NET Web Framework Quick Start Guide</a></h2>
<h2><a href="../Default.aspx">Dependency Injection</a></h2>
<div>
<form id="form1" runat="server">
<p>
The links below demonstrate dependency injection on custom handlers using <b>MappingHandlerFactory</b>.
The example code shows, how to map all url requests to spring and let MappingHandlerFactory resolve urls to
container-managed IHttpHandlerFactory or IHttpHandler objects.
</p>
<pre>
// web.config
&lt;httpHandlers&gt;
&lt;!-- map all requests to spring (just for demo - don't do this at home!) --&gt;
&lt;add verb=&quot;*&quot; path=&quot;*.*&quot; type=&quot;Spring.Web.Support.MappingHandlerFactory, Spring.Web&quot; /&gt;
&lt;/httpHandlers&gt;
// spring-objects.config
&lt;object type=&quot;Spring.Web.Support.MappingHandlerFactoryConfigurer, Spring.Web&quot;&gt;
&lt;property name=&quot;HandlerMap&quot;&gt;
&lt;dictionary&gt;
&lt;entry key=&quot;\.ashx$&quot; value=&quot;standardHandlerFactory&quot; /&gt;
&lt;!-- map any request ending with *.whatever to standardHandlerFactory --&gt;
&lt;entry key=&quot;\.whatever$&quot; value=&quot;specialHandlerFactory&quot; /&gt;
&lt;/dictionary&gt;
&lt;/property&gt;
&lt;/object&gt;
&lt;object name=&quot;standardHandlerFactory&quot; type=&quot;Spring.Web.Support.DefaultHandlerFactory, Spring.Web&quot; /&gt;
&lt;object name=&quot;specialHandlerFactory&quot; type=&quot;MySpecialHandlerFactoryImpl&quot; /&gt;
</pre>
<ul>
<li><a href="DemoHandler.ashx">DemoHandler.ashx</a></li>
<li><a href="AnotherCustomHandler.whatever">AnotherCustomHandler.whatever</a></li>
</ul>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,17 @@
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="DemoHandler.ashx">DemoHandler.ashx</a><br/>
<a href="AnotherCustomHandler.whatever">AnotherCustomHandler.whatever</a><br/>
</div>
</form>
</body>
</html>

View File

@@ -0,0 +1,30 @@
<%@ WebHandler Language="C#" Class="DemoHandler" %>
using System;
using System.Web;
public class DemoHandler : IHttpHandler
{
private string _outputText;
public string OutputText
{
get { return _outputText; }
set { _outputText = value; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("injected text:" + _outputText);
}
public bool IsReusable
{
get
{
return false;
}
}
}

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpHandlers>
<!--
the lines below map *any* request ending with *.ashx or *.whatever to the global(!) MappingHandlerFactory. Further "specialication"
of which handler to map to is done within MappingHandlerFactory's configuration - use MappingHandlerFactoryConfigurer for this (see below)
-->
<add verb="*" path="*.ashx" type="Spring.Web.Support.MappingHandlerFactory, Spring.Web" validate="true"/>
<add verb="*" path="*.whatever" type="Spring.Web.Support.MappingHandlerFactory, Spring.Web" validate="false"/>
</httpHandlers>
</system.web>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<!-- configures the global GenericHandlerFactory instance -->
<object name="mappingHandlerFactoryConfigurer" type="Spring.Web.Support.MappingHandlerFactoryConfigurer, Spring.Web">
<property name="HandlerMap">
<dictionary>
<!-- map any request ending with *.whatever to NoOpHandler -->
<entry key="\.whatever$" value="myCustomHandler" />
<entry key="\.ashx$" value="standardHandlerFactory" />
</dictionary>
</property>
</object>
<!--
uses the original System.Web.UI.SimpleHandlerFactory to create handler instances
and configures each handler using objectdefinitions matching the requestl url's filename
-->
<object name="standardHandlerFactory" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" />
<!-- defines a standard singleton that will handle *.whatever requests -->
<object name="myCustomHandler" type="MyCustomHttpHandler, App_Code">
<property name="MessageText" value="This text is injected via Spring" />
</object>
<!--
used for configuring ~/DemoHandler.ashx custom handler
note, that this is an abstract definition because 'type' is not specified
-->
<object name="DemoHandler.ashx">
<property name="OutputText">
<value>This text is injected via Spring</value>
</property>
</object>
</objects>
</spring>
</configuration>

View File

@@ -17,6 +17,8 @@
<p>The inevitable "Hello World!" example demonstrates DI for pages, usercontrols and webcontrols</p>
<h3><a href="NestedContexts/Default.aspx">Nested Contexts</a></h3>
<p>This sample shows, how contexts are nested in webapplications</p>
<h3><a href="CustomHandlers/Default.aspx">Custom HTTP Handlers</a></h3>
<p>This sample shows, how to configure your custom IHttpHandler and IHttpHandlerFactory implementations</p>
</div>
</body>
</html>