Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono and IHttpHandler

I'd like to use XSP or better mod_mono within a .Net-Project using the IHttpHandler method.

I have the following class (quite simple:

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);
    }
}

And the following 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>

It is working perfectly within IIS. http://127.0.0.1/test/kfdlsa returns 'Yeah'

Within XSP or mod_mono on Apache, I can create an index.aspx which is parsed and executed perfectly according to .Net-Framework, but it seems to be that the handler is not included within the mod_mono-Framework.

Is using IHttpHandler really implemented within Mono or shall I use another approach for collection all Requests to a certain Host and/or virtual directory.

like image 782
UllaDieTrulla Avatar asked Nov 16 '11 21:11

UllaDieTrulla


1 Answers

HTTP handlers and modules work fine in Mono.

Your problem is that your Web.config file use the syntax specific to the "Integrated Pipeline" mode of IIS. This mode doesn't exist under Apache/mod_mono. So you must use the old syntax (i.e. the one for the "Classic Pipeline" mode) and provide a <system.web/httpHandlers> section, in addition to the existing <system.webServer/handlers> section.

See this Web.config example :

<?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>

The <validation ...> tag is very important: if you forget it, IIS throws an error and complains that an unauthorized section is used in the Integrated Pipeline context.

The next step is to instruct the Apache server to transfer the handling of your files to mod_mono, like this :

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

The line AddType application/x-asp-net .rss is the important one. See the relation between path="*.rss" in Web.config and .rss extension in this line. If you want to handle all extensions, as in your example (path="*"), you must replace the line AddType application/x-asp-net .rss by ForceType application/x-asp-net.

like image 130
CedX Avatar answered Oct 01 '22 00:10

CedX