(CORE) CORE (2022)

Processing absent files by custom middleware

Middleware is new term for ASP.NET Core, at common this is ordinary Handler like this Как сделать простейший Web-handler - формирующий XML или JSON., but in Net Core it can be perform as function in App.Use.



In current project I have a lot of various Roots, see Asp Net Core Routing, Conventions, DefultUI. Site roots - ContentRootPath/StaticFilesRoot/WebRootPath/WebRootFileProvider/RazorPagesOptions.RootDirectory., by this approach I have redirect any request to another file and can make custom processing to any missing files (custom code, header or anything else).


 254:          'Route anylizer
 255:          App.Use(Async Function(context As HttpContext, NextRequestDelegate As RequestDelegate)
 256:                      Dim CurrentEndpoint = context.GetEndpoint()
 257:                      If (CurrentEndpoint Is Nothing) Then
 258:                          Debug.WriteLine($"RequestPath {context.Request.Path} endpoint nothing.")
 259:                          Dim StaticOptions As StaticFileOptions = New StaticFileOptions With {.FileProvider = New PhysicalFileProvider(Builder.Configuration("StaticFilesRoot"))}
 260:                          Dim Opt As IOptions(Of StaticFileOptions) = Options.Create(StaticOptions)
 261:                          Dim NewEnvironment As IWebHostEnvironment = Environment
 262:                          NewEnvironment.ContentRootPath = Builder.Configuration("StaticFilesRoot")
 263:                          NewEnvironment.WebRootPath = Builder.Configuration("StaticFilesRoot")
 264:                          Dim StaticMiddleware = New StaticFileMiddleware(
 265:                              Async Function()
 266:                                  'Await NextRequestDelegate(context)
 267:                                  Dim StaticFile As IFileInfo = Opt.Value.FileProvider.GetFileInfo(context.Request.Path)
 268:                                  If Not StaticFile.Exists Then
 269:                                      Await context.Response.WriteAsync("File is nothing")
 270:                                  Else
 271:                                      Await context.Response.SendFileAsync(StaticFile)
 272:                                  End If
 273:   
 274:                              End Function,
 275:                              NewEnvironment,
 276:                              Opt,
 277:                              LoggerFactory)
 278:                          Await StaticMiddleware.Invoke(context)
 279:                      Else
 280:                          Debug.WriteLine($"Endpoint: {CurrentEndpoint.DisplayName}")
 281:                          Dim Endpoint As RouteEndpoint = TryCast(CurrentEndpoint, RouteEndpoint)
 282:                          Debug.WriteLine($"RoutePattern: {Endpoint?.RoutePattern.RawText}")
 283:                          For j As Integer = 0 To CurrentEndpoint.Metadata.Count - 1
 284:                              Debug.WriteLine($"Endpoint Metadata {j}: {CurrentEndpoint.Metadata(j)}")
 285:                          Next
 286:                          Await NextRequestDelegate(context)
 287:                      End If
 288:                  End Function)
 289:   

We also can use middlware to handle unexpected exception, like we doing that in desktop application Trap unhandled exception in windows application.. This is a pattern.





Comments ( )
Link to this page: http://www.vb-net.com/StaticMiddleware/Index.htm
< THANKS ME>