2015-06-25 13 views
10

Sto giocando attorno al nuovo beta di ASPNET 5 utilizzando l'ultima versione di Visual Studio Code (v0.3). Non voglio mettere il VS2015 RC sulla mia macchina da lavoro. Ho un'app MVC6 barebone (Hello World) che ho ottenuto dopo aver scoperto le basi degli strumenti della riga di comando dn *.MVC6 Impossibile trovare app.UseStaticFiles() nell'avvio

Ora voglio servire un file statico. Ho aggiunto Microsoft.AspNet.StaticFiles al mio file project.json e ho ripristinato i pacchetti.

mio 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": { } 
    } 
} 

mio 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(); 
     } 
    } 
} 

Tutto funziona bene fino a che non includo app.UseStaticFiles() a questo punto ricevo il seguente errore:

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) 

Ho provato a utilizzare queste versioni:

  • 1.0.0-beta4
  • 1.0.0-beta6-12114

io non riesco a capire quello che sto facendo male. Qualche idea? Grazie in anticipo!

+0

VS 2015 RC ha una licenza Go Live - significa che è supportato per il lavoro di produzione. –

risposta

27

in asp.net MVC6 rc1,

aggiuntivo seguente riga nella sezione project.json di 'dipendenze':

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

In beta6/beta5, è stato cambiato come segue:

//app.UseStaticFiles(); 
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions()); 

l'aggiornamento anche da beta4 a beta5 non è una cosa più facile, ma è possibile ..

+0

Ok ha funzionato, ma ora non può caricare il tipo PathString. 'System.TypeLoadException: impossibile caricare il tipo 'Microsoft.AspNet.Http.PathString' dall'assembly 'Microsoft.AspNet.Http, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null'. –

+0

@GwendolynGoetz, assicurarsi che hai incluso le direttive necessarie (in vs2015, Ctrl +. mostra le potenziali correzioni). Se la mia soluzione ha funzionato, contrassegnare questa soluzione come risposta. – Karthick

+0

c'è un blog scritto da Miguel Alho, http://www.miguelalho.pt/upgrading-vnext-mvc6-apps-from-beta4-to-beta5/ che ti mette nella giusta direzione. – Karthick

7

In ASP.NET MVC6 RC2, aggiungere la seguente riga nella sezione project.json "dipendenze":

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

penso che è necessario installare i file libreria statica. Apri il pacchetto nuget e cerca i file statici, troverai microsoft.owin.staticfiles installarlo e funzionerà. Ho avuto lo stesso problema e dopo averlo installato ha funzionato alla grande.

4

Cambiamento, cambiamento, ..., ancora più recente:

Aggiungi project.json nella sezione "dipendenze":

"Microsoft.AspNetCore.StaticFiles": "1.0.0" 

In Startup.cs, è ora possibile utilizzare

app.UseStaticFiles(); 
+1

Mentre questo è corretto (a partire dal 2017), Nota: project.json è deprecato. Devi invece aggiungere i riferimenti nel tuo csproj. – techsaint

+0

grazie @ user1040323 – Tushortz

3

Per .net core studio codice visivo

aprire le finestre di terminale o il prompt dei comandi nella cartella principale del progetto e installare il file Microsoft.AspNetCore .pacchetto StaticFiles

dotnet Microsoft.AspNetCore.StaticFiles pacchetto aggiuntivo

per Visual Studio

aggiungere il pacchetto Microsoft.AspNetCore.StaticFiles utilizzando NuGet

ha funzionato per me

5

Se la vostra .net L'app Core utilizza csproj aggiunge la riga seguente:

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

o eseguire il comando di @Steve elencati:

dotnet add package Microsoft.AspNetCore.StaticFiles 
+0

Esattamente. Non ci sono * file * package.json da modificare più e non c'è alcun pacchetto middleware "StaticFileMiddleware". Questa roba (e Angular 2 e 4) stanno cambiando così rapidamente, la maggior parte delle risposte StackOverflow sono scadute ... –

3

Per VS 2017, ho installato utilizzando NuGet manager ma StaticFiles non era ancora accessibile. Ho persino rimosso e riaggiunto modificando il file .csproj (sostituzione XML di JSON) ma non ero ancora in grado di accedere. Alla fine ho risolto il problema salvando il progetto e riavviando VS 2017.

+2

Ho scoperto che non dovevo riavviare VS 2017, ma potevo semplicemente chiudere il progetto e riportalo di nuovo. –