Hosting ASP.NET WebAPI and Angular under one domain to avoid CORS

Viewed 608

This is not a question on how to configure CORS. I want to completely eliminate CORS requests by satisfying same origin requirement.

However, I don't want to mix angular distro files into IIS folder where WebAPI is especially that Angular needs it's own Web.config with rewriting settings.

Is there any way to host both on IIS and make sure "/" serves angular while "/api/..." hosts WebApi?

1 Answers

You can host both Frontend and API under one domain in IIS but stored in different folders. Example :

  • The root app (frontend), with physical path on C:\inetpub\front and URL rewrite according to Angular needs. One thing to add is REQUEST_URI does not match ^/api/ with your API hosted on the path /api

  • A Virtual Path inside your first one, with a virtual path on /api and physical path on C:\inetpub\api.

Both would have their own web.config, even separate application pool.

More information about virtual directories : https://docs.microsoft.com/en-us/iis/get-started/planning-your-iis-architecture/understanding-sites-applications-and-virtual-directories-on-iis

Related