1

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 ?

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • That's described in the web apps tutorials [for Razor Pages](https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.20) and [MVC](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-2.2). If you mean static files [the docs show](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2) how to do it, typically with a single call to `app.UseStaticFiles();` By default, only files under the default root folder (wwwroot) are served. The docs show how to return files outside it as well – Panagiotis Kanavos Sep 09 '19 at 07:46
  • I have checked the tutorial you provided and also this [tutorial](https://www.tutorialspoint.com/asp.net_core/asp.net_core_static_files.htm) but i still do not understand how do i serve the file from a controller ? Do i even need the file provider in that case? – Bercovici Adrian Sep 09 '19 at 08:16
  • What *is* the case? What do you want to do? Serving a static page like `index.html` is *not* the same as creating and returning a file, any file, from an API controller. Neither of these is the same as writing an SPA whose HTML is generated by Razor and calls an API either. – Panagiotis Kanavos Sep 09 '19 at 08:21
  • So i have an `angular` SPA application and i want to check user claims before i give them access to a page of the application.In order to do this i need to check the request on back end .So if the user requests a page `settings.html` from my angular app i want to pass the requests through a `Controller` in the backend and see if he has access to that page. – Bercovici Adrian Sep 09 '19 at 08:23
  • 1
    So the real question is *completely* different from the one posted here. That's called [the XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). In this case the *real* question is how to use authentication in an ASP.NET Core SPA application, which is described in [Authentication and authorization for SPAs](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0). Have you tried ASP.NET Core's angular template? – Panagiotis Kanavos Sep 09 '19 at 08:26
  • Should i mark it for close ? And repost then ? – Bercovici Adrian Sep 09 '19 at 08:48

0 Answers0