1

I have a Dot Net Core Web API developed , deployed and running in Azure. It serves as API Gateway for now. I would like to serve HTML content when user hits the root of the url in browser.

And our website content is published to azure storage behind CDN with Static Web Site. I followed the steps from below link and set it up.

https://microsoft.github.io/AzureTipsAndTricks/blog/tip203.html

How do we serve this content in my dot net core web api from azure storage and return that content to browser.

I have the below code which works for content i have locally.

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });
        }

Should i read the content of azure storage under static website blob and return it or just do the redirection to Static WebSite or CDN url.

I am trying to follow the architecture with ocelot as suggested in the answer 2 below

ASP.NET Core Api-Gateway middleware

Full Stack Brain
  • 205
  • 4
  • 11
  • Did you follow this? https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1 – Andy Aug 05 '20 at 15:22
  • yes it serves from local folder .Static files are stored within the project's web root directory. – Full Stack Brain Aug 05 '20 at 15:25
  • `I can change launchUrl value to: http://localhost:4343 to point it to root to return index html. But not sure how to return azure cdn content.` What do you mean by this statement? Do you mean you want to publish it to Azure for hosting? – Andy Aug 05 '20 at 15:29
  • I have alreay published my front end SPA static files of react project to to azure storage -> static website. i want to return that content when user hits the root of the dot net core api. – Full Stack Brain Aug 05 '20 at 15:34
  • You mean you have created `azure web app` for api, and want to serve static content by `azure static web app`. – Jason Pan Aug 05 '20 at 23:43
  • I personally tried how to better deal with this problem. I hope my answer can help you.@FullStackBrain – Jason Pan Aug 06 '20 at 04:23
  • No . I have a web api project. And then i have SPA front end web static files in Azure -> Storage -> StaticWebSite Blob. I want to serve the files from here in api. – Full Stack Brain Aug 06 '20 at 12:03
  • @FullStackBrain If you find a better solution, you can send out your answers, which can help more users. – Jason Pan Aug 07 '20 at 07:18

1 Answers1

1

I personally tried the content of your problem description, first show my code and upload it to github, you can refer to it. And suggest you read the offical document.

//for wwwroot folder
app.UseStaticFiles();

//for wwwroot outside folder

app.UseStaticFiles(new StaticFileOptions
{
     FileProvider = new PhysicalFileProvider(
         Path.Combine(env.ContentRootPath, "StaticContent")),
     RequestPath = "/StaticContent"
});

My project structure:

enter image description here

Compilation options, including custom folders.

enter image description here

All preparations for the project are completed. Here are my suggestions for your question:

  1. If you are using azure web app services, you can access the API and static resources by directly using the project. If you want to use cdn, you can set it in Networking in the webapp, which can also achieve your needs.

enter image description here

  1. If you simply use the static website in the storage, then our .net core project does not seem to be able to run directly in it, and there should be a lack of operating environment (my test results are like this, and I haven't tried other methods to solve it). enter image description here

    My suggestion is that if you really need to use the static resource files in the storage to achieve, then copy the original static resource files in, but they are two different URLs from the link URL for accessing the api.

In summary, I suggest to try my plan one, which will definitely help you very well.

Jason Pan
  • 5,844
  • 1
  • 7
  • 12