added SPRNET-711, SPRNET-713 - MappingHandlerFactory for configuring custom IHttpHandler and IHttpHandlerFactory instances
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
<httpHandlers>
|
||||
<!-- map all requests to spring (just for demo - don't do this at home!) -->
|
||||
<add verb="*" path="*.*" type="Spring.Web.Support.MappingHandlerFactory, Spring.Web" />
|
||||
</httpHandlers>
|
||||
|
||||
// spring-objects.config
|
||||
|
||||
<object type="Spring.Web.Support.MappingHandlerFactoryConfigurer, Spring.Web">
|
||||
<property name="HandlerMap">
|
||||
<dictionary>
|
||||
<entry key="\.ashx$" value="standardHandlerFactory" />
|
||||
<!-- map any request ending with *.whatever to standardHandlerFactory -->
|
||||
<entry key="\.whatever$" value="specialHandlerFactory" />
|
||||
</dictionary>
|
||||
</property>
|
||||
</object>
|
||||
|
||||
<object name="standardHandlerFactory" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" />
|
||||
|
||||
<object name="specialHandlerFactory" type="MySpecialHandlerFactoryImpl" />
|
||||
</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>
|
||||
@@ -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>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user