MVC6在Startup中找不到app.UseStaticFiles()

时间:2015-06-25 22:00:59

标签: asp.net asp.net-mvc

我正在使用最新版本的Visual Studio Code(v0.3)来玩新的ASPNET 5测试版。我不想把VS2015 RC放在我的工作机器上。我有一个准系统(Hello World)MVC6应用程序,在找出dn *命令行工具的基础知识后我已经运行了。

现在我想提供一个静态文件。我已将Microsoft.AspNet.StaticFiles添加到我的project.json文件中并恢复了包。

我的project.json:

{
    "version": "1.0.0-*",
    "webroot": "wwwroot",
    "exclude": [
        "wwwroot"
    ],
    "packExclude": [
        "**.kproj",
        "**.user",
        "**.vspscc"
    ],
    "dependencies": {
        "Kestrel": "1.0.0-*",
        "Microsoft.AspNet.Diagnostics": "1.0.0-*",
        "Microsoft.AspNet.Mvc": "6.0.0-*",
        "Microsoft.AspNet.Server.IIS": "1.0.0-*",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-*",
        "Microsoft.AspNet.StaticFiles": "1.0.0-*"
    },
    "commands": {
        "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:8080",
        "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:8080"
    },
    "frameworks": {
         "dnx451": { },
         "dnxcore50": { }
    }
}

My Startup.cs:

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;

namespace EmailViewer
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

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

一切正常,直到我包含app.UseStaticFiles(),此时我收到以下错误:

System.IO.FileLoadException: Could not load file or assembly 'EmailViewer' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
File name: 'EmailViewer' ---> Microsoft.Framework.Runtime.Roslyn.RoslynCompilationException: C:\src\fx5\EmailViewer\Startup.cs(16,13): error CS7069: Reference to type 'IApplicationBuilder' claims it is defined in 'Microsoft.AspNet.Http', but it could not be found
   at Microsoft.Framework.Runtime.Roslyn.RoslynProjectReference.Load(IAssemblyLoadContext loadContext)
   at Microsoft.Framework.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
   at Microsoft.Framework.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName)
   at dnx.host.LoaderContainer.Load(AssemblyName assemblyName)
   at dnx.host.DefaultLoadContext.LoadAssembly(AssemblyName assemblyName)
   at Microsoft.Framework.Runtime.Loader.AssemblyLoaderCache.GetOrAdd(AssemblyName name, Func`2 factory)
   at Microsoft.Framework.Runtime.Loader.LoadContext.LoadAssemblyImpl(AssemblyName assemblyName)
   at Microsoft.Framework.Runtime.Loader.LoadContext.TryLoadAssembly(LoadContext context, AssemblyName assemblyName, Assembly& assembly)
   at Microsoft.Framework.Runtime.Loader.LoadContext.ResolveAssembly(Object sender, ResolveEventArgs args)
   at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Microsoft.AspNet.Hosting.Startup.StartupLoader.FindStartupType(String startupAssemblyName, IList`1 diagnosticMessages)
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.EnsureStartup()
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.EnsureApplicationServices()
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.Start()
   at Microsoft.AspNet.Hosting.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Framework.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
   at Microsoft.Framework.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at dnx.host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env)
   at dnx.host.RuntimeBootstrapper.ExecuteAsync(String[] args)
   at dnx.host.RuntimeBootstrapper.Execute(String[] args)


Microsoft.Framework.Runtime.Roslyn.RoslynCompilationException: C:\src\fx5\EmailViewer\Startup.cs(16,13): error CS7069: Reference to type 'IApplicationBuilder' claims it is defined in 'Microsoft.AspNet.Http', but it could not be found
   at Microsoft.Framework.Runtime.Roslyn.RoslynProjectReference.Load(IAssemblyLoadContext loadContext)
   at Microsoft.Framework.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
   at Microsoft.Framework.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName)
   at dnx.host.LoaderContainer.Load(AssemblyName assemblyName)
   at dnx.host.DefaultLoadContext.LoadAssembly(AssemblyName assemblyName)
   at Microsoft.Framework.Runtime.Loader.AssemblyLoaderCache.GetOrAdd(AssemblyName name, Func`2 factory)
   at Microsoft.Framework.Runtime.Loader.LoadContext.LoadAssemblyImpl(AssemblyName assemblyName)
   at Microsoft.Framework.Runtime.Loader.LoadContext.TryLoadAssembly(LoadContext context, AssemblyName assemblyName, Assembly& assembly)
   at Microsoft.Framework.Runtime.Loader.LoadContext.ResolveAssembly(Object sender, ResolveEventArgs args)
   at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)

我尝试过使用这些版本:

  • 1.0.0-BETA4
  • 1.0.0-beta6-12114

我无法弄清楚我做错了什么。有任何想法吗?提前谢谢!

7 个答案:

答案 0 :(得分:29)

在asp.net MVC6 rc1中,

将以下行添加到project.json' s依赖关系'部分:

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

答案 1 :(得分:8)

在ASP.NET MVC6 RC2中,将以下行添加到project.json“dependencies”部分:

"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"

答案 2 :(得分:7)

在beta6 / beta5中,它已更改如下:

{{1}}

同样从beta4升级到beta5并不是最简单的事情,但它有可能......

答案 3 :(得分:6)

如果您的.net Core应用使用csproj,请添加以下行:

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />

或运行命令@Steve:

dotnet add package Microsoft.AspNetCore.StaticFiles

答案 4 :(得分:5)

适用于.net核心视觉工作室代码

在项目根文件夹中打开终端窗口或命令提示符并安装 Microsoft.AspNetCore.StaticFiles包

dotnet add package Microsoft.AspNetCore.StaticFiles

适用于Visual Studio

使用nuget

添加Microsoft.AspNetCore.StaticFiles包

为我工作

答案 5 :(得分:4)

改变,改变......,甚至更近期:

在&#34;依赖关系中添加project.json&#34;部分:

"Microsoft.AspNetCore.StaticFiles": "1.0.0"

在Startup.cs中,您现在可以使用

app.UseStaticFiles();

答案 6 :(得分:0)

我认为你需要安装静态文件库。打开nuget包并搜索静态文件,你会发现microsoft.owin.staticfiles安装它并且它会工作。我有同样的问题,安装后它工作得很好。