Spring.NET 中配置 ashx 的映射,以及其它各种映射的处理

配置 ashx 映射

Spring.NET 中可以很方便地进行各种处理程序的映射,对于 ashx 来说,支持的映射处理类为:Spring.Web.Support.DefaultHandlerFactory, 也定义在程序集 Spring.Web 中。

可以在 web.config 的配置中,增加对于 *.ashx 的映射配置。

<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>

<add verb="*" path="*.ashx" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" validate="true"/>

<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
<add verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
</httpHandlers>

然后,在配置文件中,类似页面映射,直接配置即可。

例如,我们定义了这样一个一般处理程序。

namespace Web
{
public class Handler1 : IHttpHandler
{
public string Message { get; set; }
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write( this.Message );
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

那么,在配置文件中,可以如下配置,并进行注入。

  <object name="Handler1.ashx">
<property name="Message" value="Hello, world."/>
</object>


配置多种映射规则

如果扩展名不是 .ashx ,比如说,不是 ASP.NET 直接支持的扩展名,那么,还可以使用 Spring.Web.Support.MappingHandlerFactory。

步骤稍微多了一些。

首先,在 web.config 配置文件中,将扩展名映射到这个处理程序工厂。

<add verb="*" path="*.ashx" type="Spring.Web.Support.MappingHandlerFactory, Spring.Web" validate="true"/>

然后,定义具体的映射规则,这需要通过 MappingHandlerFactoryConfigurer 来进行定义,同样可以定义在配置文件中。在它的配置中,再进行各种具体的映射,比如,扩展名为 ashx 的请求,实际上使用 DefaultHandlerFactory 进行处理等等。当然,这时候,还需要定义个 DefaultHandlerFactory 的对象来使用了。具体配置如下。

  <!-- 映射多种的扩展名 -->
<object name="mappingHandlerFactoryConfigurer" type="Spring.Web.Support.MappingHandlerFactoryConfigurer, Spring.Web">
<property name="HandlerMap">
<dictionary>
<!-- 配置 ashx 扩展名映射 -->
<entry key="\.ashx$" value="standardHandlerFactory" />
</dictionary>
</property>
</object>

<!-- 实际的处理类型 -->
<object name="standardHandlerFactory" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" />

完整的示例文件如下所示:

web.config 文件

<?xml version="1.0" encoding="utf-8"?>

<configuration>
<configSections>

<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
</sectionGroup>
</configSections>

<spring>
<!-- 引用配置文件 -->
<context>
<resource uri="~/Web.xml"/>
</context>
</spring>

<system.web>
<httpModules>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>

</httpModules>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>

<!--<add verb="*" path="*.ashx" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" validate="true"/>-->
<add verb="*" path="*.ashx" type="Spring.Web.Support.MappingHandlerFactory, Spring.Web" validate="true"/>

<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
<add verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
</httpHandlers>

<compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>

web.xml 文件

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

<!-- Referenced by main application context configuration file -->

<description>
The Northwind web layer definitions
</description>

<!-- 映射多种的扩展名 -->
<object name="mappingHandlerFactoryConfigurer" type="Spring.Web.Support.MappingHandlerFactoryConfigurer, Spring.Web">
<property name="HandlerMap">
<dictionary>
<!-- 配置 ashx 扩展名映射 -->
<entry key="\.ashx$" value="standardHandlerFactory" />
</dictionary>
</property>
</object>

<!-- 实际的处理类型 -->
<object name="standardHandlerFactory" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web" />

<!-- 一般处理程序 -->
<object name="Handler1.ashx">
<property name="Message" value="Hello, world.">
</property>
</object>

</objects>
原文地址:https://www.cnblogs.com/haogj/p/2217493.html