Basic programming, .NET technology.

11. Asp .Net core - Fundamentals - Static files

 This series is a collection of knowledge about ASP .NET Core. It just is my notes.

Part 1: Startup file.

Part 2: DI.

Part 3: Middleware.

Part 4: Host and Servers.

Part 5: Configurations.

Part 6: Environment.

Part 7: Logs.

Part 8: Error Handling.

Part 9: Routing.

Part 10: Make an HTTP Request.

Part 12: Authentication and Authorization.

Part 13: CORS.


There is how to use static files in Asp .net core:


  • Serve file in wwwroot:
app.UseStaticFiles();

  • Serve file outside wwwroot:

// using Microsoft.Extensions.FileProviders;
// using System.IO;
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "MyStaticFiles")),
RequestPath = "/StaticFiles"
});

  • Static file authorization
    • Actually, UseStaticFiles middleware is called before UseAuthorization. Most apps follow this pattern => No authorization when static file served.
    • If you want authentication before serving a static file, should do:
// Configure method
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "MyStaticFiles")),
RequestPath = "/StaticFiles"
});

AND: // ConfigureService method
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});

  • Direct browsing
  • Fileserver configuration

References:
Share:

0 nhận xét:

Đăng nhận xét

Featured Posts

Data type 3 - string type