6

我正在构建一个多租户网站,并在我的应用程序中同时使用 MapMvcAttributeRoutes 和标准 MapRoute。当我在不使用 RouteAttribute 的控制器中时,我也使用此代码成功获取子域名。我无法从使用 RouteAttribute 的任何控制器/操作中获取子域值。RouteData 集合中将不存在该值。那么当我在使用 RouteAttribute 的 Action 中时如何获取子域值?

这里是 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 });
}

例如

这将起作用:

[AllowAnonymous]
public ActionResult Register(string subdomain)
{
    ViewBag.Subdomain = subdomain;
    var vModel = new RegisterViewModel();
    return View(vModel);
}

这不起作用:

[Route("login"), AllowAnonymous]
public ActionResult Login(string returnUrl, string subdomain)
{
    ViewBag.Subdomain = subdomain; // <----------- Empty
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

大卫

4

0 回答 0