6

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:

Others:

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.
Community
  • 1
  • 1
Twenty
  • 5,234
  • 4
  • 32
  • 67

2 Answers2

3

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

  • Would the URL in the browser stick with the subdomain e.g. `blog.example.com` or would it actually switch the url to example.com/blog/$request_uri? – Twenty Jul 24 '19 at 06:08
  • 1
    @Twenty: That should depend on how you configure nginx. iirc it can also rewrite response urls – Tseng Jul 24 '19 at 07:11
  • Well if it doesn't rewrite per default it would be perfect. Thanks in advance, will mark it as soon as I can verify it working. – Twenty Jul 24 '19 at 08:16
  • 1
    @Twenty In this case there are two options, `return directive` and `rewrite directive` on `NGINX Rewrite Rules` (i gave return directive example) please check them. – Mustafa Salih ASLIM Jul 24 '19 at 08:17
  • Thanks for letting me know! – Twenty Jul 24 '19 at 08:31
3

To give an update to this whole situation, with the release of .Net Core 3 you can now make use of the RequireHost method.

This would look something like the following:

app.UseEndpoints(endpoints =>
{
    endpoints.MapAreaControllerRoute(
                name: "Home",
                areaName: "Home",
                pattern: "{controller=Home}/{action=Index}")
             .RequireHost("localhost:5001", "sub.domain.com");
}

If you remove the Area in the pattern parameter, like in the example, you can achieve exactly that. It is still somewhat hacky, but a lot cleaner. Note, that you would have to put a RequireHost on all of the endpoints in order to get proper default route matching.

Twenty
  • 5,234
  • 4
  • 32
  • 67