4

In an ASP.NET Web Application ... MVC (not core), I could in the web.config file add this...

<system.webServer>
    <handlers>
      <add name="JavaScriptHandler"
         path="*.js"
         verb="*"
         preCondition="integratedMode"
         type="System.Web.StaticFileHandler" />
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler"
           path="*"
           verb="*"
           preCondition="integratedMode"
           type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

And then I could place a javascript file in same folder as the view files.

Can I somehow do the same in ASP.Net Core 1.1?

Thanks

2
  • 2
    don't think so. They should be inside wwwroot Commented Feb 4, 2017 at 21:28
  • what i have done is during build step, i have set instructions which run gulp utility that move the required js files from node or bower modules to wwwroot /libs or your desired path. its quite simple practice i came to know Commented Feb 24, 2017 at 7:26

1 Answer 1

6
+100

Here is the official Microsoft documentation: Working with Static Files in ASP.NET Core

As part of the OWIN pipeline, no static files are rendered by default including those in the "wwwroot" folder. Where things used to be handled in web.config, now everything goes through the OWIN pipeline. You have to add the StaticFile Middleware which will allow those to be rendered. The default implementation allows all files under "wwwroot" to be rendered.

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
}

The documentation is provided in the link above for adding additional locations to allow files to be rendered from. It's not explicit, but you can look at the implementation of the StaticFile Middleware at Github to get ideas for custom implementations.

The below code will add StaticFiles Middleware to only allow javascript files to be rendered from the "Views" folder. You can add other files by adding items to the Dictionary.

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();

    // Render only .js files in "Views" folder
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"Views")),
            RequestPath = new PathString("/Views"),
            ContentTypeProvider = new FileExtensionContentTypeProvider(
                new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { ".js", "application/javascript" },
                })
        }
    );
}

Your only other alternative is to add custom grunt (or gulp) tasks to copy your javascript files in your "Views" folders into new folders created in your "wwwroot" folder. You can see some examples here of compiling TypeScript code and doing something similar. Using Grunt Tasks

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great solution, just note that when you deploy your app, the Views are sometimes compiled and the "Views" folder is gone. You either have to create a build step that will copy the files, or set "Copy to output directory"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.