简体   繁体   中英

How to route all subdomain on Net core 3.1

I want to use a different controller when users enter a subdomain. I´m using RequireHost .

How can it works this with any domain? For example I'm using domain.test just for development, but in production I have another.

Startup.cs

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Subdomain}/{action=Index}/{id?}").RequireHost("*.domain.test")


    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

RequireHost is almost the same as adding [Host("...")] attributes everywhere, except that they only apply to that route.

Evaluation of Host rules seems to occur in HostMatcherPolicy . Which should treat "*.domain.test" as matching all subdomains, but not the domain itself. You will need to add "domain.test" if you want that to match too.

However, you do have a second route that can match everything. I suspect you will need to explicitly list the valid hosts for your default route. Or split your controllers into different areas to ensure they only match the expected rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM