【问题标题】:UseStaticFiles() throws error "Connection refused"UseStaticFiles() 抛出错误“连接被拒绝”
【发布时间】:2016-12-09 12:58:36
【问题描述】:

我在 asp.net 核心中提供静态文件时遇到问题。我真的很陌生。只是按照复数视觉的教程并停留在这里。

我在 wwwroot 文件夹中添加了 index.html,并在 Json 中添加了静态文件的依赖项,但似乎仍然不起作用。任何帮助将不胜感激。

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;
using Microsoft.Extensions.Logging;

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, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseStaticFiles();
          //  app.Run(async (context) =>
          //  {
          //      await context.Response.WriteAsync("Hello World!");
          //  });
        }
    }
}

这是我的 project.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",

    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

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

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

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

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

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

【问题讨论】:

    标签: asp.net-core asp.net-core-1.0


    【解决方案1】:

    为了让您的 Web 应用程序在用户不必完全限定 URI 的情况下提供默认页面,请从 Startup.Configure 调用 UseDefaultFiles 扩展方法,如下所示。

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

    UseDefaultFiles 必须在 UseStaticFiles 之前调用才能提供默认文件。

    参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files#serving-default-files

    【讨论】:

      猜你喜欢
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      相关资源
      最近更新 更多