Mapping Subdomains to Areas in ASP.Net Core 3

2019-07-26 22:09发布

问题:

What I want to achieve

  • Mapping my normal domain e.g.example.com to no area if possible.
  • Mapping my subdomain e.g. blog.example.com to a area named blog.

What I have found

There are actually quite a lot of posts regarding to this topic, especially mapping subdomains to areas.

From SO:

  • area-and-subdomain-routing
  • asp-net-core-mapping-subdomains-to-areas

Others:

  • danylkoweb.com
  • benjii.me
  • web.archive.org

And there are probably even more.

Problem

But there is one big problem, in ASP.Net Core 3 they changed a lot of things, one of them being the routing in general, see mircosoft's devblog. Basically they changed it so everything should now be endpoints.

All the classes e.g. MvcRouteHandler and interfaces e.g. IRouter are basically obsolete now, at least from my understanding. After a while googling around and diggin' in the GitHub repositories I couldn't find anything useful.

Additional information

  • I am using SDK 3.0.100-preview6-012264, but trying to upgrade to SDK 3.0.100-preview7-012821 as soon as possible.
  • I am using a reserve proxy (nginx) which passes the request to the ASP.Net Core Server.

回答1:

You said all requests pass on nginx but nothing said about nginx redirection, did you try to use nginx to do that, just redirect the sub-domain to domain using /etc/nginx/nginx.conf.

server {
   server_name sub.domain.co;
   location / {
   return 301 $scheme://domain.co/BlogSite$request_uri;
  }
}

(BlogSite is your area routing on ASP.Net Core Server.)