Search code examples
asp.net-coresubdomainrazor-pages

Razor pages shows empty page when changing Request.Path with a middleware


I'm using a middleware for subdomains. Middleware changes Request.Path as follows:

URL:

http://sub1.example.com

Request.Path:

/SubdomainPages/sub1

This works. But when URL contains a page (http://sub1.example.com/SomePage for instance), I'm getting a blank page. And when I use NotFound() action result in OnGet handler same thing occurs (I have /Pages/Errors/404.cshtml).

When I don't use middleware, pages and redirecting error page with NotFound() work well.

This is subdomain middleware:

public class SubdomainRoutingMiddleware
{
    private readonly RequestDelegate _next;

    public SubdomainRoutingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        string[] Subdomains = new string[] { "sub1", "sub2" };
        var parts = context.Request.Host.Value.Split('.');
        if (parts.Length > 2)
        {
            if (Subdomains.Contains(parts[0]))
            {
                if (context.Request.Path.Value != "/")
                    context.Request.QueryString = context.Request.QueryString.Add("sayfa", context.Request.Path.Value.Substring(1));
                context.Request.Path = "/SubdomainPages/" + parts[0];
            }
        }

        await _next.Invoke(context);

    }
}

And this is Startup.cs file:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

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

        app.UseStatusCodePages();

        app.UseStatusCodePagesWithReExecute("/Errors/{0}");

        app.UseStaticFiles();

        app.UseMiddleware<SubdomainRoutingMiddleware>();

        app.UseMvc();
    }
}

EDIT:

Request.Path changed with middleware when URL contains page, path becomes as follows:

URL:

http://sub1.example.com/SomePage

Request.Path:

/SubdomainPages/sub1/SomePage

/SomePage exists but /SubdomainPages/sub1/SomePage does not exist. This is a part of error.

I also added app.UseStatusCodePages() to Startup.cs. But still not working/calling 404 page when URL contains subdomain. And not showing status message like Status Code: 404; Not Found. Still showing blank page.


Solution

  • I found solution.

    I also changed Request.Host in middleware. Now everything has been working as I expected.

        public async Task Invoke(HttpContext context)
        {
            string[] Subdomains = new string[] { "sub1", "sub2" };
            var parts = context.Request.Host.Value.Split('.');
            if (parts.Length > 2)
            {
                if (Subdomains.Contains(parts[0]))
                {
                    if (context.Request.Path.Value != "/")
                        context.Request.QueryString = context.Request.QueryString.Add("sayfa", context.Request.Path.Value.Substring(1));
                    context.Request.Path = "/SubdomainPages/" + parts[0];
                    // Change host (without subdomain)
                    context.Request.Host = new HostString("example.com");
                }
            }
    
            await _next.Invoke(context);
    
        }