简体   繁体   English

ASP.NET Core 2.0静态文件,但是.html不起作用?

[英]ASP.NET Core 2.0 static files, but .html doesn't work?

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: 我正在尝试创建一个最小的ASP.NET Core 2.0应用程序,因此我在VS 2017中创建了一个空的Web应用程序,然后运行:

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

My Startup.cs looks like: 我的Startup.cs看起来像:

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: 然后,我使用以下内容创建一个wwwroot/test.html

<!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. 然后,我创建一个wwwroot/test.txt ,其内容与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. 因此,这似乎是一个简单的设置,应该为wwwroot下的任何静态文件提供超过400种已知MIME类型的服务。

Now when I run this project with F5, it opens to the root URL and displays "Hello World!" 现在,当我使用F5运行该项目时,它将打开到根URL并显示“ 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. 如果将/test.txt添加到URL,则会再次按预期显示上面显示的HTML。 If you change the URL to /test.html you do not get the HTML file as expected, but instead the standard "Hello World!" 如果将URL更改为/test.html ,则不会按预期方式获取HTML文件,而是获取标准的“ 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! 现在停止调试,将test.html更改为test.htm ,再试一次, test.htm可以正常工作!

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 . 我发疯了,也许他们只是没有将.html与FileExtensionContentTypeProvider中的“ text / html”相关联,但是不, 确实如此 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? 还是我唯一一个疯了,这个例子对其他所有人都适用?

Thanks to greg84's comment above, I retraced my steps and I know how everything got borked. 多亏了greg84的上述评论,我得以追溯自己的脚步,而且我知道一切都变得令人厌烦。 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 ". 我进入了“项目属性”>“调试”面板以设置默认的启动URL,但是有一次我错误地将/test.html添加到“ App URL”参数中,因此它看起来像“ 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. .vs/applicationhost.config已将其存储为第二个 application / virtualDirectory映射,这破坏了对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. 当然,在我进行调查时,这些设置实际上在任何地方都不可见,因此我勉强只在单个.html页面上出现了损坏的行为,这对我来说绝对没有意义。

And of course, moving the project to a second machine simply carried the .vs/applicationhost.config file with it, so the same behaviour appeared. 当然,将项目移至另一台计算机只是带有.vs / applicationhost.config文件,因此出现了相同的行为。

Thanks for the suggestions everyone! 谢谢大家的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM