Mono和IHttpHandler

时间:2011-11-16 21:05:22

标签: asp.net .net apache mono mod-mono

我想使用IHttpHandler方法在.Net-Project中使用XSP或更好的mod_mono。

我有以下课程(非常简单:

public class Class1 : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        var result = "<h1>Yeah</h1>";
        var bytes = Encoding.UTF8.GetBytes(result);

        context.Response.Write(result);
    }
}

以下web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers accessPolicy="Read, Execute, Script">
            <add name="Class" path="*" verb="*" type="IISHost.Class1" resourceType="Unspecified" preCondition="integratedMode" />
        </handlers>
    </system.webServer>
    <system.web>
        <compilation defaultLanguage="c#" />
    </system.web>
</configuration>

它在IIS中完美运行。 http://127.0.0.1/test/kfdlsa返回'是'

在Apache上的XSP或mod_mono中,我可以创建一个index.aspx,它根据.Net-Framework完美地解析和执行,但似乎处理程序不包含在mod_mono-Framework中。

使用IHttpHandler是否真的在Mono中实现,或者我是否应该使用另一种方法来收集对某个主机和/或虚拟目录的所有请求。

1 个答案:

答案 0 :(得分:11)

HTTP处理程序和模块在Mono中运行良好。

您的问题是您的Web.config文件使用特定于IIS的“集成管道”模式的语法。 Apache / mod_mono下不存在此模式。因此,除了现有的<system.web/httpHandlers>部分之外,您必须使用旧语法(即“经典管道”模式的语法)并提供<system.webServer/handlers>部分。

请参阅此Web.config示例:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <httpHandlers>
            <add path="*.rss" verb="*" type="CedricBelin.Web.FeedHandler" />
        </httpHandlers>
    </system.web>

    <system.webServer>
        <handlers>
            <add name="Feed" path="*.rss" verb="*" type="CedricBelin.Web.FeedHandler" />
        </handlers>

        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
</configuration>

<validation ...>标记非常重要:如果您忘记了它,IIS会抛出错误并抱怨在Integrated Pipeline上下文中使用了未经授权的部分。

下一步是指示Apache服务器将文件处理转移到mod_mono,如下所示:

<VirtualHost *:80>
    ServerName mono.localhost
    DocumentRoot "/Library/WebServer/Documents/MonoTest"
    AddType application/x-asp-net .rss
</VirtualHost>

AddType application/x-asp-net .rss是重要的一行。请参阅此行中path="*.rss" Web.config.rss扩展名之间的关系。 如果您要处理所有扩展程序(例如path="*"),则必须将行AddType application/x-asp-net .rss替换为ForceType application/x-asp-net