【问题标题】:No output after build编译后无输出
【发布时间】:2015-11-27 07:21:39
【问题描述】:

我正在探索 ASP。 NET 5 并遵循一些教程。

我正在使用 Visual Studio 2015。

我打开了项目>ASP NET Web 应用程序>ASP NET 5 模板>空。

我做了一个简单的构建,它显示“Hello World”

所以我在 wwwroot 目录中添加了 index.html

并注释掉 startup.cs 代码中的 hello world 行

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

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
    //    app.UseIISPlatformHandler();

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

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

现在,当我构建和运行时,我得到一个空白屏幕。它应该显示 index.html 但事实并非如此。

编辑:使用 app.UseStaticFiles();仅适用于 /index.html 而不是一般情况。

回答我发现所有的 ASPNET 测试版都有 app.UseStaticFiles();为静态文件服务。现在 1.0.0-rc1-final 发布了,我们必须使用 app.UseDefaultFiles(); 以及

【问题讨论】:

  • 你想使用 MVC 显示 Hello world 吗?
  • index.Html 上有什么要显示的吗?
  • 是的 index.html 有基本的正文和文本。不,不使用 MVC。到目前为止,只是为了显示一个静态文件。 (稍后我会做 MVC)

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

默认情况下,ASP.NET 5 管道不会提供文件,即使它们位于 wwwroot 文件夹中。您必须通过添加对 Microsoft.AspNet.StaticFiles 包的引用,然后将静态文件中间件添加到管道中来明确告诉它您需要静态文件:

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

之后,您可以导航到&lt;url&gt;/index.html 并获取预期的文件。

【讨论】:

  • 您提到的内容适用于 Web,但 IIS 需要单独配置,我可以在没有 /index.html 的情况下运行 index.html
猜你喜欢
  • 2016-12-31
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2015-05-01
相关资源
最近更新 更多