How to serve Static WebSite content through Dot Net core Web APi in Azure

dotnetcoreapi 1 Reputation point
2020-08-05T18:36:51.163+00:00

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. Please note that my SPA refers to the same API for data.

I could do this easily if my front end project in the same directory as the web api project but our build pipeline publishes it to the azure storage to leverage CDN.

I have the below code i copied from msdn blog which gives any idea how to include static content.

    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();
        });
    }

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

https://stackoverflow.com/questions/53477140/asp-net-core-api-gateway-middleware

My main question is

Should i read the content of azure storage under static website blob and return it which i feel a bit of processing

or

just do the redirection to Static WebSite or CDN url link.

or

host the website as azure static website or CDN ? If hosteed as Azure Static Website does it support https custom domain/host name mapping ?

Azure Stack Hub
Azure Stack Hub
An extension of Azure for running apps in an on-premises environment and delivering Azure services in a datacenter.
188 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,758 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 28,106 Reputation points Microsoft Employee
    2020-08-07T16:42:29+00:00

    @dotnetcoreapi my response was too long for a comment, so I had to post as an answer. We are addressing this issue in a future revision but please see my response below.

    For Ocelot, have you referenced this doc and repo? Seems in line with what you're trying to achieve. If you're utilizing container orchestration for your API services, let me know. For now, there are three options I can think of. You'll have to determine which one to explore for your architecture.

    1. Look into using middleware to route "/" requests to your API to your static website.
    2. In your App Gateway, you can create routing rules to direct traffic between your API and static site based on URL
    3. Host the VueJS SPA inside your API project

    I have not had an opportunity to POC this but let me know if you do run into any problems.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.