How to test a subdomain / webpage in VS, with localhost

Claudia Pinson 46 Reputation points
2023-02-20T15:19:24.94+00:00

Hi,

I'm building a ASP.NET MVC app, using VS2019. For this app, the url in Prod should be: www.myapp.com; and the functionality for this application requires a list of pages customied for each client, for example:
For client 1, they should go to: client1.myapp.com;

For client 2, they should go to: client2.myapp.com;

For client 3, they should go to: client3.myapp.com;

etc.

Each of these subdomains should go to a different, customized page each client.

Currently in VS, for local testing, I'm using: "http://localhost:44351/", for "www.myapp.com".
My 1st question is: how do I test the subdomains for multiple clients within VS2019, with the "localhost"? Can I test "client1.localhost:44351/" in VS2019?

Also, question #2: what is the best way to structure the backend code, controller etc. for these subdomains? I'm thinking about how to define a route for this and add to the RouteCollection. I can retrieve "client1", "client2" etc from the database, and pass it in as a parameter. However, I'm not clear on how to direct "client1.exmplae.com" to the correct controller action, with "client1" as a parameter passed in?

BTW - these pages for subdomains will be very similar, with only a few places showing client-specific info. So I want to use 1 controller action to handle all of them, with the clientID passed in as parameter, so I can use the clientID to display different client-specific info as needed.

An example would be very helpful.

Please advise.
Thanks,

Claudia

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,276 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,716 Reputation points Microsoft Vendor
    2023-02-21T07:58:40.24+00:00

    Hi @Claudia Pinson,

    I think you can refer to this post which has detailed steps.

    how do I test the subdomains for multiple clients within VS2019, with the "localhost"? Can I test "client1.localhost:44351/" in VS2019?

    You can achieve this by adding ports to localhost via the hosts file that resides at this path:

    (C:\Windows\System32\drivers\etc\hosts),You must be an administrator (right click on notepad, run as administrator),

    And append lines like the following to it:

    127.0.0.1    example.com
    127.0.0.1    subdomain.example.com
    

    what is the best way to structure the backend code, controller etc. for these subdomains?

    You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom Route:

    More information

    public class ExampleRoute : RouteBase
    {
    
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var url = httpContext.Request.Headers["HOST"];
            var index = url.IndexOf(".");
    
            if (index < 0)
                return null;
    
            var subDomain = url.Substring(0, index);
    
            if (subDomain == "user1")
            {
                var routeData = new RouteData(this, new MvcRouteHandler());
                routeData.Values.Add("controller", "User1"); //Goes to the User1Controller class
                routeData.Values.Add("action", "Index"); //Goes to the Index action on the User1Controller
    
                return routeData;
            }
    
            if (subDomain == "user2")
            {
                var routeData = new RouteData(this, new MvcRouteHandler());
                routeData.Values.Add("controller", "User2"); //Goes to the User2Controller class
                routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller
    
                return routeData;
            }
    
            return null;
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            //Implement your formating Url formating here
            return null;
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.