简体   繁体   English

ASP.NET 5项目不提供index.html

[英]ASP.NET 5 Project not serving index.html

I'm following along with a beginner's tutorial for ASP.NET 5 and MVC6, and I've run into a wall regarding my first ever project. 我正在关注ASP.NET 5和MVC6的初学者教程,并且遇到了有关我的第一个项目的困惑。 I have an app that should simply serve index.html as a static file from the wwwroot folder. 我有一个应仅将index.html作为wwwroot文件夹中的静态文件提供服务的应用程序。 However, when I run the project, I get a 404 error. 但是,当我运行项目时,出现404错误。

My code is as follows: 我的代码如下:

startup.cs 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 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%" ]
  }
}

Add app.UseStaticFiles(); 添加app.UseStaticFiles(); in your Configure method 在您的Configure方法中

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

Add this Dependancy to project.Json file 将此依赖项添加到project.Json文件

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

I've found the answer to this question. 我已经找到了这个问题的答案。 I needed to change project.json to look like this: 我需要将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"
  },

and change my startup.cs file to include the following line: 并将我的startup.cs文件更改为包含以下行:

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

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

暂无
暂无

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

相关问题 带有 React 模板的 ASP.NET Core 返回 index.html - ASP.NET Core with React template returns index.html 在 asp.net core 中将 index.html 设置为默认页面 - Setting index.html as default page in asp.net core ASP.Net Core - 动态生成 index.html - ASP.Net Core - Dynamically generate index.html ASP.NET 5 Beta 7(Web Tools 2015 Beta7) - 未提供wwwroot / index.html - ASP.NET 5 Beta 7 (Web Tools 2015 Beta7) - wwwroot/index.html not served c#:Asp.net WebApi到index.html的默认路由 - c#: Asp.net WebApi default route to index.html 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC 在asp.net Core 2项目中为路线提供角度服务 - Serving angular with routes in asp.net core 2 project ASP.NET Core在项目目录之外提供文件 - ASP.NET Core serving a file outside of the project directory 带有角度模板的 asp.net core 3.1 无法再响应 404 并始终重定向到 spa index.html 页面 - asp.net core 3.1 with angular template can no longer respond with 404 and always redirect to spa index.html page 如何在TFS 2017 Build之后通过TFS Build Job中的Shell脚本自动递增asp.net核心应用程序版本并将其显示在index.html页脚中 - How to auto increment asp.net core application version via shell script in TFS Build Job after TFS 2017 Build and show it in the footer of index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM