【问题标题】:ASP.NET 5 Project not serving index.htmlASP.NET 5 项目不提供 index.html
【发布时间】:2016-05-18 18:00:09
【问题描述】:

我正在学习 ASP.NET 5 和 MVC6 的初学者教程,但在我的第一个项目中遇到了困难。我有一个应用程序应该只是将index.html 作为wwwroot 文件夹中的静态文件提供服务。但是,当我运行该项目时,出现 404 错误。

我的代码如下:

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace TheWorld
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        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.UseStaticFiles();

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

project.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

【问题讨论】:

    标签: c# asp.net json iis


    【解决方案1】:

    添加 app.UseStaticFiles();在您的配置方法中

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

    将此依赖项添加到 project.Json 文件中

    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"
    

    【讨论】:

    • 我刚刚编辑了我的帖子,因为我忘了提及——当我添加那行代码时,我收到了这个错误:i.imgur.com/aJB5oPf.png
    • 我添加了依赖项"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",它解决了我在编辑器中的错误,但是当我尝试从wwwroot 文件夹提供index.html 时,我的应用程序仍然是404。
    【解决方案2】:

    我已经找到了这个问题的答案。我需要将project.json 更改为如下所示:

      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0-rc2-3002702",
          "type": "platform"
        },
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"
      },
    

    并更改我的startup.cs 文件以包含以下行:

    public void Configure(IApplicationBuilder app)
    {
        app.UseFileServer();
    
        //app.Run(async (context) =>
        //{
        //    await context.Response.WriteAsync("Hello World!");
        //});
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多