【问题标题】:Asp.net Core 2 - Static files not working on prod but they work through iis expressAsp.net Core 2 - 静态文件在 prod 上不起作用,但它们通过 iis express 工作
【发布时间】:2017-10-02 00:18:32
【问题描述】:

在 iis express 上本地运行我的 asp.net core 2 应用程序(web api)时,调用静态资源似乎可以工作,但是当部署到 iis 8 时,我得到一个 404 尝试访问。 这是我的设置,我在本地将资源放在 wwwroot 中,而 startup.cs、program.cs 是高一级目录。

部署时,所有编译/发布的dll和资源都部署到网站的wwwroot。现在我的 api 可以正常工作,例如我定义了一个路由,例如

 [HttpGet]
 [Route("api/misc/categories")]
 public IActionResult Categories()
 {
     return Ok(DbContext.Categories);
 }

所以调用 http://cryptocal.imetasoft.com/api/misc/categories 会返回应有的类别,但 http://cryptocal.imetasoft.com/index.html 会返回 404,即使 index.html 和 cryptocal.dll 都在我的网络服务器(产品)的 wwwroot 中

我相信我已经正确设置了所有内容。 我添加了一个nuget包到Microsoft.AspNetCore.StaticFiles v2.0

.csproj 内容

 <Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
    <PackageReference Include="morelinq" Version="2.7.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>

Program.cs 无更改(生成模板的默认值)

使用 Microsoft.AspNetCore; 使用 Microsoft.AspNetCore.Hosting;

namespace CryptoCal
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoCal.Models;
using Microsoft.EntityFrameworkCore;
using CryptoCal.Support;

namespace CryptoCal
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<CryptoDateContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CryptoCalDatabase")));
            services.Configure<GeneralSettings>(Configuration.GetSection("GeneralSettings"));
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc(x =>
            {

            });
        }
    }
}

【问题讨论】:

  • 我不希望 DLL 文件和静态文件位于同一个文件夹中,如果你的意思是你所说的那样。正确部署后,DLL 可能位于根目录中,而静态文件位于 wwwroot 子文件夹中。也许这就是你的意思?
  • 在网站面板中创建站点时,它为 iis 的物理路径放置的根设置为 wwwroot(我想这就是它的作用)。所以实际上我的站点的“根”是一个名为 wwwroot 的文件夹,在里面我将有另一个 wwwroot 作为核心。这是我的第一个核心部署,在此之前所有内容、dll、bin 等都被转储到 wwwroot 中。
  • 我不确定“网站面板”是什么;这可能是对您的问题进行的相关编辑。
  • 真的应该是无关紧要的,因为 iis 点:C:\HostingSpaces\admin\cryptocal.imetasoft.com\wwwroot 并且在根目录下的那个文件夹中我有 dll,然后是另一个名为 wwwroot 的文件夹用于静态文件

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


【解决方案1】:

这是我的疏忽,wwwroot 的内容应该高于 dll 所在的位置,我不知道为什么我首先将它们放在同一个文件夹中。 @k3davis 首先是正确的

IIS 指向\

\dll \wwwroot 用于所有静态文件

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多