Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Subdomains to Areas in ASP.Net Core 3

Tags:

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.
like image 336
Twenty Avatar asked Jul 23 '19 22:07

Twenty


People also ask

What is subdomain mapping?

A subdomain is a domain that is part of a larger domain. For example, if the root domain is example.com, some subdomains that could be used are blog.example.com, news.example.com, updates.example.com. You can map a subdomain or root domain to your Typepad blog.

What is subdomain routing?

Subdomain routing is the same as routing prefixing, but it's scoped by subdomain instead of route prefix. There are two primary uses for this. First, you may want to present different sections of the application (or entirely different applications) to different subdomains.

What is a subdomain name?

A subdomain is a prefix added to a domain name to separate a section of your website. Site owners primarily use subdomains to manage extensive sections that require their own content hierarchy, such as online stores, blogs or support platforms. Subdomains function as a separate website from its domain.


1 Answers

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.)

like image 57
Mustafa Salih ASLIM Avatar answered Oct 18 '22 20:10

Mustafa Salih ASLIM