6

マルチテナント Web サイトを構築しており、アプリケーションで MapMvcAttributeRoutes と標準の MapRoute の両方を使用しています。また、RouteAttribute を使用しないコントローラーにいるときに、このコードを使用してサブドメイン名を正常に取得します。RouteAttribute を使用するコントローラー/アクションからサブドメインの値を取得できません。値は RouteData コレクションから存在しません。では、RouteAttribute を使用するアクション内にいるときにサブドメイン値を取得する方法は??

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