0

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?}");
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hans Felix Ramos
  • 4,264
  • 3
  • 16
  • 40
  • Possible duplicate: https://stackoverflow.com/questions/57172884/mapping-subdomains-to-areas-in-asp-net-core-3 – Bruno Jul 14 '20 at 20:43

1 Answers1

1

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.

Jeremy Lakeman
  • 9,515
  • 25
  • 29