locked
UseBasePath seems to have no affect RRS feed

  • Question

  • User-2098916251 posted

    I have a minimal ASP.NET Core 3 MVC Hello World WebApp generated by Visual Studio 2019 that prints the current date time.

    I have deployed this to my Azure Kubernetes cluster and access it with Ingress. However, Ingress is blocking access to the static files like jquery.min.js and bootstrap.css. This is only a problem when running with Ingress and it is not a problem when running with without kubernetes/ingress.

    The symptoms I'm experiencing are described perfectly here: https://www.billbogaiv.com/posts/net-core-hosted-on-subdirectories-in-nginx

    I cannot make the solution of using "app.UseBasePath" to work. It seems to have no affect at all! Here is my Startup.cs file (see below).

    As you can see, I've been also experimenting also with app.use(...) inside the Configure function with no success. I've also experimented with putting the call to app.UseBasePath at the beginning, middle and end of the Confiure function and nothing works. When running directly from Visual Studio 2019 with no docker/kubernetes/ingress, I was expecting to have to type http://localhost/kuberneteshelloaspnet but that gives me an error (404). Instead http://localhost works. This behavior is a problem with Kubernetes/ingress which wants to prefix all the URLs.

    Thank you

    Siegfried

    namespace KubernetesHelloWorldASPNET
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                //app.Use((context, next) => { context.Request.PathBase = "/kuberneteshelloaspnet"; return next(); });            
                
                app.UseStaticFiles();
                
                app.UseRouting();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
                app.UsePathBase("/kuberneteshelloaspnet");
            }
        }

    Thursday, September 17, 2020 1:03 AM

Answers

  • User-2098916251 posted

    I had been using https://docs.microsoft.com/en-us/azure/aks/ingress-basic that demonstrates the rewrite rule.

    The problem with using the rewrite rule is that it does not accommodate multiple web-apps that have different copies of static files like jquery.min.js.

    The solution for C#/VB.NET ASP.NET Core programmers is to use UseBasePath.

    I wish the above tutorial would explain these different options and their peril and merits. Obviously UseBasePath won't work with java or python webapps (although there might be counterparts).

    I did not realize that since the rewrite rule executes first to remove the root segment of the path, then there is nothing for the UseBasePath to remove and it seems to have no affect.

    The solution is to remove the rewrite rule after having added a call to UseBasePath.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 29, 2020 2:00 PM

All replies

  • User-194899966 posted

    Hi Sigfried,

    Have you used the following annotation:

    • nginx.ingress.kubernetes.io/rewrite-target: /

    That makes that any request to /app, goes to the application root (/) at the backend. But if you use /app pointing to a Kubernetes service, your application should have the /app endpoint as well (/app/images/*, /app/jquery.min.js, etc.).

    Let us know how that works for you.

    Tuesday, September 22, 2020 10:23 PM
  • User-474980206 posted

    As all the middleware does is extract the prefix from the path, the middleware must come early, certainly before routing or static files if they need the prefix removed. 

    Wednesday, September 23, 2020 1:47 AM
  • User-2098916251 posted

    I had been using https://docs.microsoft.com/en-us/azure/aks/ingress-basic that demonstrates the rewrite rule.

    The problem with using the rewrite rule is that it does not accommodate multiple web-apps that have different copies of static files like jquery.min.js.

    The solution for C#/VB.NET ASP.NET Core programmers is to use UseBasePath.

    I wish the above tutorial would explain these different options and their peril and merits. Obviously UseBasePath won't work with java or python webapps (although there might be counterparts).

    I did not realize that since the rewrite rule executes first to remove the root segment of the path, then there is nothing for the UseBasePath to remove and it seems to have no affect.

    The solution is to remove the rewrite rule after having added a call to UseBasePath.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 29, 2020 2:00 PM