简体   繁体   English

如何在 Net core 3.1 上路由所有子域

[英]How to route all subdomain on Net core 3.1

I want to use a different controller when users enter a subdomain.当用户进入子域时,我想使用不同的 controller。 I´m using RequireHost .我正在使用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.例如,我将 domain.test 用于开发,但在生产中我有另一个。

Startup.cs启动.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. RequireHost几乎与在任何地方添加[Host("...")]属性相同,只是它们仅适用于该路由。

Evaluation of Host rules seems to occur in HostMatcherPolicy .主机规则的评估似乎发生在HostMatcherPolicy中。 Which should treat "*.domain.test" as matching all subdomains, but not the domain itself.应该将“*.domain.test”视为匹配所有子域,而不是域本身。 You will need to add "domain.test" if you want that to match too.如果您希望它也匹配,您将需要添加“domain.test”。

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.或者将你的控制器分成不同的区域,以确保它们只匹配预期的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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