azureazure-static-web-appazure-sdkazure-static-website-hosting

Stop and start an Azure SWA using the C# SDK


I have a Static Web App deployed to Azure. I need to temporarily stop it or redirect it to something like offline.html using the Azure C# SDK. I have an instance of a GenericResource class representing the SWA. How can I do that?

Looks like I can delete the SWA, but I need to be able to more or less instantly bring it back.


Solution

  • I tried checking the Azure.ResourceManager.AppService C# SDK and there's no method present to stop the static site.

    Refer below:-

    enter image description here

    Reference MS Document

    Also, Azure static web app portal does not support any option to Stop the Azure Static Web app.

    enter image description here

    But you can redirect your SWA to offline.html page when you do not want your main page to serve content. By routing your main page to offline page and updating your Program.cs to route all traffic from your root path to offline.html like below:-

    using BlazorApp3.Components;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddRazorComponents();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseAntiforgery();
    
    app.Use(async (context, next) =>
    {
        if (context.Request.Path == "/")
        {
            context.Response.Redirect("/offline.html");
        }
        else
        {
            await next();
        }
    });
    
    app.MapRazorComponents<App>();
    
    app.Run();
    

    enter image description here

    My github repository1 for above output.

    Another way is to add Offline.Razor or Offline.html page in your source code and route your traffic there like below:-

    My github repository2 with Offline.razor output below

    enter image description here