2017-10-30 3 views
3

Dies ist meine erste .net Core-Anwendung. Und Problem haben. Das Problem ist, dass ich neue Vorlage hinzugefügt wurde, um Anwendung und Google Console zeigt Fehler, der nicht in der Lage finden CSS und JSCSS, Bilder, JS lädt nicht asp.net Core :(

**

  • Appsetting.Json

**

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Server=.;Database=MusicSite;Trusted_Connection=True;MultipleActiveResultSets=true" 



    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Warning" 
    } 
    }, 

    "dependencies": { 
     "bootstrap": "3.3.6", 
     "jquery": "2.2.0" 
    } 

    } 

die alle CSS und Js Ordner werden in der Lösung enthalten

enter image description here

Fehler, die ich

**

  • Console Fehler

in Console App bin immer **

enter image description here

**

  • Layoutseite

**

 <!-- style --> 
     <link rel="stylesheet" href="css/animate.css/animate.min.css" type="text/css" /> 
     <link rel="stylesheet" href="css/glyphicons/glyphicons.css" type="text/css" /> 
     <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css" type="text/css" /> 
     <link rel="stylesheet" href="css/material-design-icons/material-design-icons.css" type="text/css" /> 
     <link rel="stylesheet" href="css/bootstrap/dist/css/bootstrap.min.css" type="text/css" /> 
     <!-- build:css css/styles/app.min.css --> 
     <link rel="stylesheet" href="css/styles/app.css" type="text/css" /> 
     <link rel="stylesheet" href="css/styles/style.css" type="text/css" /> 
     <link rel="stylesheet" href="css/styles/font.css" type="text/css" /> 

     <link rel="stylesheet" href="libs/owl.carousel/dist/assets/owl.carousel.min.css" type="text/css" /> 
     <link rel="stylesheet" href="libs/owl.carousel/dist/assets/owl.theme.css" type="text/css" /> 
     <link rel="stylesheet" href="libs/mediaelement/build/mediaelementplayer.min.css" type="text/css" /> 
     <link rel="stylesheet" href="libs/mediaelement/build/mep.css" type="text/css" /> 
     <!-- endbuild --> 

    <script src="libs/jquery/dist/jquery.js"></script> 
    <!-- Bootstrap --> 
    <script src="libs/tether/dist/js/tether.min.js"></script> 
    <script src="libs/bootstrap/dist/js/bootstrap.js"></script> 
    <!-- core --> 
    <script src="libs/jQuery-Storage-API/jquery.storageapi.min.js"></script> 
    <script src="libs/jquery.stellar/jquery.stellar.min.js"></script> 
    <script src="libs/owl.carousel/dist/owl.carousel.min.js"></script> 
    <script src="libs/jscroll/jquery.jscroll.min.js"></script> 
    <script src="libs/PACE/pace.min.js"></script> 
    <script src="libs/jquery-pjax/jquery.pjax.js"></script> 
    <script src="libs/mediaelement/build/mediaelement-and-player.min.js"></script> 
    <script src="libs/mediaelement/build/mep.js"></script> 
    <script src="scripts/player.js"></script> 
    <script src="scripts/config.lazyload.js"></script> 
    <script src="scripts/ui-load.js"></script> 
    <script src="scripts/ui-jp.js"></script> 
    <script src="scripts/ui-include.js"></script> 
    <script src="scripts/ui-device.js"></script> 
    <script src="scripts/ui-form.js"></script> 
    <script src="scripts/ui-nav.js"></script> 
    <script src="scripts/ui-screenfull.js"></script> 
    <script src="scripts/ui-scroll-to.js"></script> 
    <script src="scripts/ui-toggle-class.js"></script> 
    <script src="scripts/ui-taburl.js"></script> 
    <script src="scripts/app.js"></script> 
    <script src="scripts/site.js"></script> 
    <script src="scripts/ajax.js"></script> 
    <!-- endbuild --> 

**

  • Startup.cs

**

namespace MusicSite 
{ 
    public class Startup 
    { 
     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

      if (env.IsDevelopment()) 
      { 
       // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709 
       builder.AddUserSecrets<Startup>(); 
      } 

      builder.AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     public void ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      services.AddDbContext<ApplicationDbContext>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

      services.AddIdentity<ApplicationUser, IdentityRole>() 
       .AddEntityFrameworkStores<ApplicationDbContext>() 
       .AddDefaultTokenProviders(); 

      services.AddMvc(); 

      // Add application services. 
      services.AddTransient<IEmailSender, AuthMessageSender>(); 
      services.AddTransient<ISmsSender, AuthMessageSender>(); 
     } 

     // 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) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 
      app.UseStaticFiles(); 

      // Add MVC to the request pipeline. 
      app.UseMvc(); 
      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseDatabaseErrorPage(); 
       app.UseBrowserLink(); 
      } 

      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      app.UseIdentity(); 

      // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
      }); 
     } 
    } 
} 
+1

Können Sie Ihren Startup.cs-Code hinzufügen? Oder überprüfen Sie, ob Sie Middleware StaticFiles() hinzugefügt haben. – Anuraj

+0

Ich füge meinen Startcode hinzu – Agha

Antwort

6

In ASP.NET-Kern, sind standardmäßig statische Dateien nur aus dem wwwroot Ordner serviert. Das heißt, wenn Sie versuchen, auf eine Datei von Libs Verzeichnis zuzugreifen, wird es nicht funktionieren.

Die gute Nachricht ist, Sie können den Speicherort der statischen Datei konfigurieren, wie Sie möchten. Aktualisieren Sie also Ihre Configure Methode in startup.cs.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
                  ILoggerFactory loggerFactory) 
{ 
    // Your existing code goes here 

    app.UseStaticFiles(); 

    // This will add "Libs" as another valid static content location 
    app.UseStaticFiles(new StaticFileOptions() 
    { 
     FileProvider = new PhysicalFileProvider(
       Path.Combine(Directory.GetCurrentDirectory(), @"Libs")), 
     RequestPath = new PathString("/libs") 
    }); 
} 

Die PhysicalFileProvider Klasse ist im Microsoft.Extensions.FileProviders Namespace definiert. Sie sollten also eine using-Anweisung zu der in Ihrer Klasse Startup.cs hinzufügen.

using Microsoft.Extensions.FileProviders; 

Jetzt können die Dateien von yourSiteName/libs/somejsfile.js zugegriffen werden. Außerdem sollten Sie diesen Skripten den Pfad mit ~/ voranstellen. Die Tilda-Zeichen (~) zeigen an, dass es sich um den App-Root handelt.

<script src="~/libs/jquery/dist/jquery.js"></script> 

Wie ich bereits erwähnt, ist die wwwroot ist ein spezieller Ordner Ihre statischen Vermögenswerte zusammen zu halten. Sie können also auch erwägen, Ihr libs Verzeichnis unter wwwwroot zu verschieben und dann sollte alles ohne die oben erwähnte benutzerdefinierte Konfiguration funktionieren.

+0

Wir wollten nur Dateien von 'wwwroot' hosten, aber, seltsam genug, scheint es, wenn wir ASP.NET Core im Full Framework hosten, dass die UseStaticFile() initiiert werden muss mit explizit auf die 'wwwroot', das heißt' app.UseStaticFiles (neu StaticFileOptions() { FileProvider = new PhysicalFileProvider (Path.Combine (Directory.GetCurrentDirectory() @ "wwwroot")) }); ' – veljkoz