Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I allow an MIME extension map in ASP.NET vNext?

Background

I have a piece of LESS code that needs to be compiled at runtime with Less.js -- it calculates some things via JavaScript -- so I can't use the task runner, etc.

In my index.html, I have:

<head>
   ...
   <link rel="stylesheet/less" href="assets/less/DynamicHeight.less" />
   ...
   <script type="text/javascript" src="lib/less/less.js"></script>
   ... 
</head>

Problem

Less.js appears unable to find the file:

LESS.js barfing because IIS can't serve the file

And when I try to access the file directly, I see:

enter image description here

Question

How can I add the configuration that will allow this less file to be downloaded? Am I still able to use web.config files with vNext, or do I need to do something with config.json instead?

Lead 1: Should I use Owin?

Thinking this might be the right path but I'm pretty unfamiliar.

I see a number of tutorials out there, such as K. Scott Allen's, which reference code such as:

    public void Configuration(IAppBuilder app)
    {
        var options = new StaticFileOptions
        {
            ContentTypeProvider = new FileExtensionContentTypeProvider()
        };
        ((FileExtensionContentTypeProvider)options.ContentTypeProvider).Mappings.Add(
            new KeyValuePair<string, string>(".less", "text/css"));
        app.UseStaticFiles(options);
    }

However, it appears that in its current version, asp.net is looking for a signature of Configure(IApplicationBuilder app) instead.

The IApplicationBuilder class doesn't have a method along the lines of UseStaticFiles -- it only has a signature of IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware).

I have a feeling that this is likely the right path to solve the issue -- I just can't find out how to propertly configure the IAppliationBuilder to map the MIME extension.

like image 833
SeanKilleen Avatar asked Feb 01 '15 15:02

SeanKilleen


2 Answers

Okay, I believe I figured it out.

Step 1: Add the appropriate library for static files

In ASP.NET vNext, this is Microsoft.Aspnet.StaticFiles.

In your project.json file, add the following under "dependencies":

"Microsoft.AspNet.StaticFiles": "1.0.0-beta2"

This adds the static middleware method that you can use later.

Step 2: Configure the app to use Static Files

Add the using statement at the top:

using Microsoft.AspNet.StaticFiles;

At this point, the app.UseStaticFiles method will be available, so your Configure method can look as follows:

public void Configure(IApplicationBuilder app)
{
    var options = new StaticFileOptions
    {
        ContentTypeProvider = new FileExtensionContentTypeProvider()
    };
    ((FileExtensionContentTypeProvider)options.ContentTypeProvider).Mappings.Add(
        new KeyValuePair<string, string>(".less", "text/css"));
    app.UseStaticFiles(options);
}

And voila! I get text when browsing to .less files, and no more error is appearing from LessJS.

like image 188
SeanKilleen Avatar answered Sep 22 '22 05:09

SeanKilleen


In .NET Core 1.0.1, SeanKileen answer is still good. The following is a simple code rewrite:

public void Configure(IApplicationBuilder app, ...)

    var contentTypeProvider = new FileExtensionContentTypeProvider();
    contentTypeProvider.Mappings[".map"] = "application/javascript";
    contentTypeProvider.Mappings[".less"] = "text/css";

    app.UseStaticFiles(new StaticFileOptions()
    {
        ContentTypeProvider = contentTypeProvider
    });

The above code EXTENDS the default mapping list (see the source), which already has ~370 mappings.

Avoid using the FileExtensionContentTypeProvider constructor overload that takes a dictionary (as suggested by JHo) if you want those 370 default mappings.

like image 39
Gerardo Grignoli Avatar answered Sep 22 '22 05:09

Gerardo Grignoli