1

This is making me tear my hair out. I'm trying to create a minimal ASP.NET core 2.0 app, so I create an empty web application in VS 2017, and I run:

> uninstall-package Microsoft.AspNetCore.All
> install-package Microsoft.AspNetCore
> install-package Microsoft.AspNetCore.StaticFiles

My Startup.cs looks like:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseStaticFiles();

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
}

I then create a wwwroot/test.html with these contents:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <h1>HTML</h1>
    <p>Hello world!</p>
</body>
</html>

I then create a wwwroot/test.txt with the same contents as test.html. So this appears to be is a bare bones setup that should serve any static files under wwwroot for over 400 known MIME types.

Now when I run this project with F5, it opens to the root URL and displays "Hello World!" as expected, indicating it hit the "terminal middleware delegate". If you add /test.txt to the URL you get the HTML displayed above, again as expected. If you change the URL to /test.html you do not get the HTML file as expected, but instead the standard "Hello World!" message.

I've tried this on two separate machines, same results. Now stop debugging, change test.html to test.htm, try again and test.htm works as expected!

An insane thought occurred to me, that perhaps they simply didn't associate .html with "text/html" in the FileExtensionContentTypeProvider, but nope, there it is. I even tried creating a custom type provider explicitly with this association, but that didn't work either.

Can anyone clue me in as to what is going on here? Or am I the only one going insane here and this example works fine for everyone else?

8
  • learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Apr 21, 2018 at 17:45
  • Static files are working fine except for .html. How about actually providing helpful feedback. Commented Apr 21, 2018 at 17:47
  • 1
    How did you create the file? I've just created a new project and can't reproduce the problem. The .html extension works, everything else returns the Hello World message. Commented Apr 21, 2018 at 17:49
  • You say the file is called test.html, but in your description of the URL, you've written text.html. Commented Apr 21, 2018 at 17:52
  • I created the file in visual studio, like any other file. It can't be related to the file itself, because if I rename the extension it works. This originally happened in another project, and I started a whole new project to reproduce the problem. I have another thought since you created a project, go into the project properties > Debug panel. In the App URL where it says something like localhost:56945, add test.html on the end and try that. It's a mistake that breaks it, but even after removing it, I think it leaves .html static files broken. Commented Apr 21, 2018 at 17:53

1 Answer 1

1

Thanks to greg84's comment above, I retraced my steps and I know how everything got borked. I had gone into the project properties > Debug panel to setup the default launch URL, but at one point I mistakenly added /test.html to the "App URL" parameter, so it looked like "http://localhost:57661/test.html".

After realizing that I had actually intended to add it to the "Launch Browser" section, I corrected that, but damage already done. The .vs/applicationhost.config had already stored it as a second application/virtualDirectory mapping, which broke all references to test.html.

Of course, these settings are not actually visible anywhere as I was investigating, so I chugged along only getting broken behaviour on my single .html page which made absolutely no sense to me.

And of course, moving the project to a second machine simply carried the .vs/applicationhost.config file with it, so the same behaviour appeared.

Thanks for the suggestions everyone!

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

Comments

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.