1

私は MVC4 を使用しており、ホストの詳細を取得するために、global.asax 内にカスタム ルートを作成しました。

ユーザーが「theirbusiness.mydomain.com」経由でログインできるようにする製品をセットアップしました

ユーザーが「randomstore.mydomain.com」を参照したときに、ストアが存在しない場合にデータベース内に実際のストアが既に作成されていることを確認し、ユーザーをページにリダイレクトしたいと考えています。

これまでのところ、

  public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.Add(new SubDomainRoute());
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Process", action = "Index", id = UrlParameter.Optional },
            constraints: null,
            namespaces: new[] { "WebApplication.Controllers" }
        );
    }

    protected void Application_Start()
    {
        log4net.Config.XmlConfigurator.Configure();
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}


public class SubDomainRoute : RouteBase
{
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
        log.Info(string.Format("GetRouteData"));
        log.Info(DateTime.Now);
        var url = httpContext.Request.Headers["HOST"];
        var index = url.IndexOf(".");

        if (index < 0)
            return null;
        log.Info(url);
        var subDomain = url.Substring(0, index);
        //check 


        log.Info(subDomain);

        return null;
    }

変数 subDomain を取得してデータベース内でチェックする最善の方法は何ですか。SubDomainRoute クラス内でそれをしようとするのは正しくないと感じます

4

1 に答える 1

0

すべてのリクエストに対してチェック/リダイレクトを実行したい場合は、Application_BeginRequestそれ自体で簡単に実行できます。

protected void Application_BeginRequest(object sender, EventArgs e)
{
  var context = ((HttpApplication)sender).Context;
  var host = context.Request.Headers["HOST"];

  // check the store is created in database and redirect if not exist.
}
于 2012-06-12T07:13:43.520 に答える