Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Subdomains

I have hosting and domain like that:

www.EXAMPLE.com

I've created few subdomains like that:

www.PAGE1.EXAMPLE.com
www.PAGE2.EXAMPLE.com
www.PAGE3.EXAMPLE.com
... etc...

All of these subdomains point to one and the same ASP.NET MVC 5 Application.

I want to make system which will load data depending of subdomain. Example:

I have Article object which could be a Auto Review or Game review or Book Review etc...

I would like to www.auto.example.com load data where type of article is Auto, to www.book.example.com I would like to load data with type Book etc.

There will be many types of the pages.

What is best practise to do that?

The top level domain www.example.com should display something else. It would be main page for the others.

like image 525
Ellbar Avatar asked Dec 16 '13 11:12

Ellbar


2 Answers

You can do this by writing a custom Route. Here's how (adapted from Is it possible to make an ASP.NET MVC route based on a subdomain?)

public class SubdomainRoute : RouteBase
{

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var host = httpContext.Request.Url.Host;
        var index = host.IndexOf(".");
        string[] segments = httpContext.Request.Url.PathAndQuery.Split('/');

        if (index < 0)
            return null;

        var subdomain = host.Substring(0, index);
        string controller = (segments.Length > 0) ? segments[0] : "Home";
        string action = (segments.Length > 1) ? segments[1] : "Index";

        var routeData = new RouteData(this, new MvcRouteHandler());
        routeData.Values.Add("controller", controller); //Goes to the relevant Controller  class
        routeData.Values.Add("action", action); //Goes to the relevant action method on the specified Controller
        routeData.Values.Add("subdomain", subdomain); //pass subdomain as argument to action method
        return routeData;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        //Implement your formating Url formating here
        return null;
    }
}

Add to the route table in Global.asax.cs like this:

routes.Add(new SubdomainRoute());

And your controller method:

public ActionResult Index(string subdomain)
{
    //Query your database for the relevant articles based on subdomain
    var viewmodel = MyRepository.GetArticles(subdomain);
    Return View(viewmodel);
}
like image 114
Paul Taylor Avatar answered Oct 17 '22 09:10

Paul Taylor


This is something I have wanted to do with ASP.NET MVC for a long time, but... This is not a concern that ASP.NET MVC is responsible for. This is a server concern (IIS). What you need to do is allow for wildcard subdomains on your IIS server and point them to your one application.

Then you can do something like this with the HttpContext:

HttpContext.Current.Request.Url.Host // user1.yourwebsite.com

Then you just need to parse that and push it into your ASP.NET MVC app anyway you see fit:

  • Push it into Session
  • Update the current route data and push a value in
  • Etc....

The choice is really up to you.

Note: The downside here is that this makes local development increasingly difficult, so you might want to mock up a way to fake a subdomain in your application.

like image 6
Khalid Abuhakmeh Avatar answered Oct 17 '22 07:10

Khalid Abuhakmeh