简体   繁体   中英

Serve static files from a folder inside wwwroot

I have following ASP.NET Core project structure:

.
├── Controllers
├── Dockerfile
├── Models
├── Program.cs
├── Properties
├── README.md
├── Services
├── Startup.cs
├── Views
├── appsettings.json
├── bundleconfig.json
├── project.json
├── web.config
└── wwwroot

Inside wwwroot , I have set up an Aurelia project using Aurelia cli . It has following structure:

.
├── aurelia-app
├── css
├── images
├── js
└── temp.html

and my aurelia-app has the index.html file which I want to serve(when I browse to localhost:5000 , in a similar way if it was in wwwroot )

Here's what my Startup.cs configure() method looks like:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseFileServer(new FileServerOptions
    {
        EnableDefaultFiles = true,
        EnableDirectoryBrowsing = true,
    });

   app.UseMvc();
}

What should I change so that upon loading the base url, it looks for the index.html file in wwwroot/aurelia-app directory?

I'm not sure this works or not, but you can try.

var options = new DefaultFilesOptions
{
   RequestPath = RequestPath = new PathString("/wwwroot/aurelia-app or /aurelia-app")
};
app.UseDefaultFiles(options);
app.UseStaticFiles();

This has more to do with the default 'index' file of your website than with serving static files. I guess you want to serve all the files in wwwroot , not just what's inside wwwroot/aurelia-app . So scoping your static files middleware to wwwroot/aurelia-app won't work.

Your best option is probably to set the default application URL to http://localhost:5000/aurelia-app/index.html in your launchSettings.json (Project -> Properties -> launchSettings.json):

"iisExpress": {
  "applicationUrl": "http://localhost:5000/aurelia-app/index.html",
}

You should apply such setting in your IIS website or Azure Web App as well. For example: Azure Web App设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM