dbomb101 dbomb101 - 18 days ago 5
C# Question

route configuration subdomain

I've been trying to create a link that includes the subdomain to look like this batman.website.com, but instead it generates this website.com/?subdomain=batman

I'm generating the link through this method

@Html.RouteLink("Link", new { controller = "home", subdomain = activity.From.Username, id = activity.PostId, action = "post" })


and my routing routeconfig class looks like this

public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new SubdomainRoute());
}
}


the subdomain route is heavily based off of this http://benjii.me/2015/02/subdomain-routing-in-asp-net-mvc/

Could someone point me in the right direction to format the link correctly

Answer

If you're using MVC for authentication, you could do something like this, straight into the view:

<a href="http://\\@HttpContext.Current.User.Identity.Name\\.website.com">Test Link</a>

Another way (if your authentication isn't based on MVC) would be to set the username at the View Controller on the ViewBag and then set it to display in the view:

Controller:

ViewBag.VarName = userName;

View:

<a href="http://\\@ViewBag.VarName\\.website.com">Test Link</a>

Another question that gives more detail and examples: How to get current user, and how to use User class in MVC5?

Hopefully some of this points you in the right direction!

Comments