Search code examples
asp.net-mvcvideomonomime-typesxsp

Mime types for XSP Mono Webserver


Does anyone know if it is possible to add mime types to the XSP Webserver?

I'm working on a local Asp.net MVC application (Windows) that uses XSP as a lightweight server.

The problem is that XSP does not deliver the correct mime types for some files. One example is mp4, which gets the mime type "application/octet-stream" instead of "video/mp4".

This causes IE9/IE10 to offer a download dialog on those files instead of playing the videos natively.

I've tried to configure the mime type in web.config (which works for other servers like iis express)

<mimeMap fileExtension=".mp4" mimeType="video/mp4" />

But with no success.

This FAQ page http://www.mono-project.com/FAQ:_ASP.NET sounds not so promising:

... and it is also missing features like mime-type configuration and any other features that people expect from a web server.

But maybe there are undocumented ways to support different mime types...


Solution

  • With some support from a Mono developer I found a solution which is not specific to Mono.

    Defining an HttpHandler:

    web.config

    <httpHandlers>
          <add verb="*" path="*.mp4" type="MyApp.Helpers.MimeTypeHandler" />
    </httpHandlers>
    

    MimeTypeHandler.ashx.cs

    namespace MyApp.Helpers
    {
        public class MimeTypeHandler : IHttpHandler
        {
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                string absolutePath = context.Server.MapPath("~" +context.Request.Url.PathAndQuery);
                var length = new System.IO.FileInfo(absolutePath).Length;
                context.Response.CacheControl = "Public";
                context.Response.AddHeader("Content-Length", length.ToString());
                context.Response.ContentType = "video/mp4";
                context.Response.WriteFile(absolutePath);
                context.Response.Flush();
                context.Response.Close();
            }
        }
    }