Search code examples
c#asp.net-core-mvc

ASP.NET Core HOSTNAME Configuration


I have a need to define specific configuration properties based on the HOST that is called in my C# ASP.NET Core MVC Application.

I have a webapp (mywebapp) that does functions the same for each user, but pulls data from user defined tables. I need to be able to detect the subdomain the user is calling to determine what set of tables to pull data from.

Example: if a user calls https://site1.mywebapp.com, the data will be pulled from tables defined only for site1.

Whereas a user calling site2.mywebapp.com would be served data from site2 tables.

This all worked with the global.asax page but is seems with ASP.NET Core, I can no longer detect the users domain at processing.

How can this be done in C# ASP.NET Core?

It would be great to have a base class that all requests would go through where I could detect the subdomain and control the tables a user's data comes from.


Solution

  • Thank you Mark. I was able to find a solution using the information you provided.

    1. Create a new BaseController and override OnActionExecuting. This method gets called before every action.

    2. Have my HomeController inherit the BaseController. The HomeController now has access to the SITE the user is looking for.