tags:

views:

1595

answers:

3

I'm trying to use the Server class from Cassini to include a basic web server in my own application. I just started playing around with it to get familiar with the way the server works and I setup a simple app that is as follows:

    static void Main(string[] args)
    {
        Server server = new Server(80, "/", @"C:\Projects\");
        server.Start();
        Console.ReadLine();
        server.Stop();
    }

It lets me browse through the directories, however if I try to click on a file, a C# source file (*.cs) for example, it gives the following error:

Server Error in '/' Application.

This type of page is not served.

Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cs' may be incorrect.
Please review the URL below and make sure that it is spelled correctly.

I tried searching for that error text in the Cassini libraries, but didn't find anything.

Where is this error coming from? How can I make it serve up any file? I know it's meant to do asp.net and HTML, but I want it to also server up any file like a normal server would.

+2  A: 

Hi Adam,

.cs files and many source code types are prevented from rendering because they are handled by ASP.NET's forbidden file handler.

This is initially configured in the following setting in the master web.config in c:\windows\microsoft.net\v2.0.50727\CONFIG\web.config:

Look for in the <httpHandlers> section, you see settings like:

<add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>

Generally this is a good idea because it prevents casual browsing of your source code which may contain sensitive data such as connection strings.

You should be able to remove this restriction in your app's local web.config by doing:

<configuration>
   <system.web>
      <httpHandlers>
         <remove verb="*" path="*.cs"/>
      </httpHandlers>
   </system.web>
</configuration>

I probably wouldn't recommend doing this on a web facing production environment.

HTH
Kev

Kev
A: 

@Kev

I didn't realize that it was set at a global level like that.

One question though... My app (at least right now as it's a test) is just a console app and doesn't have a web.config or even app.config file. Can I just add it and paste in what you showed? If not, what do I have to do to set that up?

Adam Haile
I haven't had any time to play with Cassini but I'd expect you'd be able to pop a web.config file in your c:\projects folder and it'll work. I'm assuming you have .aspx pages in that folder?
Kev
Also....this article on MSDN by Dino Esposito delves into hosting cassini: http://msdn.microsoft.com/en-us/magazine/cc188791.aspx how much is still relevant, I've no idea.
Kev
You may also want to choose a different http port (say 8080 or whatever) if you've already got IIS installed. This might reduce a small world of pain if IIS and Cassini start fighting over port 80. Just a thought.
Kev
A: 

I'm having the same problem with classic ASP when mapping all requests thru ASP.NET. Does anybody know how to make ASP work on WinXP IIS if you map everything to aspnet_isapi.dll? It should just forward the .asp request to IIS but it doesn't.

Thank you!