How to serve pages using fileprovider from controller?

87 views Asked by At

I have a Asp Net Core and while i have some controllers that represent simple API calls i also want to use FileProvider's for serving pages.

How can i redirect a request for a page to a controller (check credentials) , and serve it ? I must also branch the pipeline so that i can serve simple json's.

So far my fileprovider is served to everyone requests it.How do i use it from a controller (adding additional guard logic on the request) ?

Startup

  public void Configure(IApplicationBuilder app) {

            app.MapWhen(x => x.Request.Path.Value.Contains("pages"), x => {
               PhysicalFileProvider provider = new PhysicalFileProvider([some path]);
               DefaultFilesOptions options = new DefaultFilesOptions() {
                  DefaultFileNames = new List<string> { "index.html" },
                  FileProvider = provider
               };

               x.UseDefaultFiles(options);
               x.UseStaticFiles(new StaticFileOptions {
                  RequestPath = "index.html",
                  FileProvider = provider
               });
               x.UseMvc();
            });
            app.UseMvc();
}

PageController

public class PageController:Controller {
        [HttpGet]
        [Route("/pages/index.html")]
        public async Task<File> ServeIndexAsync() {
            //logic to check if user has access

         }
        [HttpGet]
        [Route("/pages/data.html")]
        public async Task<File> ServeDataAsync()
        {
        }
}

Api Controller

This is a normal controller whose logic would get used whenever someone requests a non-page request:

public NormalController:Controller
{
   [HttpGet]
   [Route("/get-data")]
   public async Task<string> GetDataAsync()
   { 
      return "some data";
   }

}

P.S I do not know what return type the PageController methods should have if they return files ?

0

There are 0 answers