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

8
  • Did you follow this? learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Aug 5, 2020 at 15:22
  • yes it serves from local folder .Static files are stored within the project's web root directory. Commented Aug 5, 2020 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? Commented Aug 5, 2020 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. Commented Aug 5, 2020 at 15:34
  • You mean you have created azure web app for api, and want to serve static content by azure static web app. Commented Aug 5, 2020 at 23:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.