1

There are a lot of material written about Subdomain routing in ASP.NET MVC. Some of them use Areas as target for subdomains other use another Controllers.

There are some of them:

Subdomains for a single application with ASP.NET MVC

Asp.Net MVC 2 Routing SubDomains to Areas

MVC 3 Subdomain Routing

MVC-Subdomain-Routing on Github

They do all explain how to accept and route requests with subdomain.

But:

  1. None of them explains how to generate URLs with subdomain. I.e. I tried @Html.RouteLink("link to SubDomain", "SubdomainRouteName") but what it ignores subdomain and generates url without it

  2. How to deal with the same names of controllers from different areas. All those solutions (they use namespaces for these purpose) throw exception that exist several controllers and suggest using namespaces :)

Purpose: create mobile version of site using subdomain

Community
  • 1
  • 1
Cheburek
  • 2,103
  • 21
  • 32

1 Answers1

0

I've wrote a post on how I use subdomain routing in my application. The source code is available on the post, but I'll try to explain how I did my custom RouteLink method.

The helper method uses the RouteTable class to get the Route object based on the current Url and cast it to a SubdomainRoute object.

In my case all routes are defined using the SubdomainRoute and everytime I need to add a link to some other page I use my custom RouteLink helper, this is why I consider this cast safe. With the SubdomainRoute variable available I'm able to get the subdomain name and then build the Url using the UriBuilder class.

This is the code I'm currently using.

public static IHtmlString AdvRouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, object routeValues, object htmlAttributes)
{
    RouteValueDictionary routeValueDict = new RouteValueDictionary(routeValues);
    var request = htmlHelper.ViewContext.RequestContext.HttpContext.Request;
    string host = request.IsLocal ? request.Headers["Host"] : request.Url.Host;
    if (host.IndexOf(":") >= 0)
        host = host.Substring(0, host.IndexOf(":"));

    string url = UrlHelper.GenerateUrl(routeName, null, null, routeValueDict, RouteTable.Routes, htmlHelper.ViewContext.RequestContext, false);
    var virtualPathData = RouteTable.Routes.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, routeName, routeValueDict);

    var route = virtualPathData.Route as SubdomainRoute;

    string actualSubdomain = SubdomainRoute.GetSubdomain(host);
    if (!string.IsNullOrEmpty(actualSubdomain))
        host = host.Substring(host.IndexOf(".") + 1);

    if (!string.IsNullOrEmpty(route.Subdomain))
        host = string.Concat(route.Subdomain, ".", host);
    else
        host = host.Substring(host.IndexOf(".") + 1);

    UriBuilder builder = new UriBuilder(request.Url.Scheme, host, 80, url);

    if (request.IsLocal)
        builder.Port = request.Url.Port;

    url = builder.Uri.ToString();

    return htmlHelper.Link(linkText, url, htmlAttributes);
}

private static IHtmlString Link(this HtmlHelper htmlHelper, string text, string url, object htmlAttributes)
{
    TagBuilder tag = new TagBuilder("a");
    tag.Attributes.Add("href", url);
    tag.InnerHtml = text;
    tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
goenning
  • 6,514
  • 1
  • 35
  • 42
  • 1
    @nonintanon archived post link: http://web.archive.org/web/20120623210212/http://geekstuff.cc/2011/12/02/subdomain-routing-on-asp-net-mvc – rovsen Mar 26 '13 at 13:11