Asp.net Core 2 - 静态文件不能生成prod,但它们通过iis express工作

时间:2017-10-02 00:18:32

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

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

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

 [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都在我的网络服务器(prod)上的wwwroot中

我相信我已经正确设置了一切。 我在Microsoft.AspNetCore.StaticFiles v2.0

添加了一个nuget包

.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 =>
            {

            });
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

IIS指向\

\的DLL \ wwwroot所有静态文件