Search code examples
azureasp.net-corednssubdomain

Subdomain ASP.Net API project under another ASP.Net MVC project


I'm building a website/API. I have two ASP.Net Core 3.1 projects:

  • MyProject.Site
  • MyProject.Api

They are both hosted in Azure as App Services:

  • myproject.api.azurewebsites.net
  • myproject.site.azurewebsites.net

Finally myproject.site.azurewebsites.net is mapped to my custom domain, https://myproject.com.

What I'd like to do is set up https://api.myproject.com to resolve to myproject.api.azurewebsites.net, but I can't see how to associate the two now that I have the primary domain linked to the site.

I have an A record for api with my domain registrar, but I noticed within Azure that both app services have the same IP address listed under the Custom Domains pane. (I would expect them to have different IPs since they're different sites.)

When I go to set up a subdomain on the domain registrar, it's asking for a directory, which I assume would be something like /api to indicate https://api.myproject.com would reroute to https://myproject.com/api. But that wouldn't that mean that I'd need to expose my API project through the /api route on my site project?

I'm new to domains and basic infrastructur-y stuff, so explanations of solutions are appreciated.

(I also had originally considered putting the API and the site in the same project. Then I probably wouldn't have this problem. But I soon found that I needed different authentication methods between them, so I split them to accommodate this requirement.)


Solution

  • As @SmithMart 's comment, you just need to create a CNAME record for the api subdomain that points to another App Service myproject.api.azurewebsites.net in your domain management.

    You don't worry about the directory, DNS will help resolve the name api.myproject.com to your real site myproject.api.azurewebsites.net.

    You could follow the Tutorial: Map an existing custom DNS name to Azure App Service. You will get