Reputation: 1250
I'm building a multi-tenant website and I use both MapMvcAttributeRoutes and the standard MapRoute in my application. I also use this code to get the subdomain name successfully when I'm in a controller that doesn't use the RouteAttribute. I can't get the subdomain value from any controllers/actions that use the RouteAttribute. The value will not exist from the RouteData collection. So how to get the subdomain value when I'm inside an Action that use a RouteAttribute??
Here the code of the RegisterRoutes:
public static void RegisterRoutes(RouteCollection routes)
{
const string mainNamespace = "Presentation.Website.Controllers";
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); // Activate attribute Routing
// This will add the parameter "subdomain" to the route parameters
// TODO: Do the same for MapMvcAttribute! but how... ?
routes.Add(new SubdomainConfig(new[] { "www", "yourdomain", "mail" }, new[] { mainNamespace }, CoreSettings.SubDomainKey));
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { mainNamespace });
}
e.g
This will work:
[AllowAnonymous]
public ActionResult Register(string subdomain)
{
ViewBag.Subdomain = subdomain;
var vModel = new RegisterViewModel();
return View(vModel);
}
This won't work:
[Route("login"), AllowAnonymous]
public ActionResult Login(string returnUrl, string subdomain)
{
ViewBag.Subdomain = subdomain; // <----------- Empty
ViewBag.ReturnUrl = returnUrl;
return View();
}
David
Upvotes: 6
Views: 644