0

请在下面查看我的要求。

我想将用户重定向到具有以下条件的帐户/登录页面。

  1. 如果用户输入例如http://example.com/Customer1

customer1 是客户,我将其保存在一个配置表中,其中我有 customer1 的连接字符串

所以基本上 customer1 我需要检查这是否存在于数据库中,然后重定向到 /Customer1/account/login 页面。

请让我知道这是否可能?如果是,我如何设置或检查这个 map.route?

public ActionResult Login()
        {
            string CurrentURL = Request.Url.AbsoluteUri;

                var subdomain = CurrentURL.Split('/')[5];
                var getDB = (from c in dbcontext.Configuration
                             where c.CustomerName == subdomain
                             select new
                                 {
                                     DBName = c.CustomerDBName,
                                     DBUserName = c.CustomerDBUserName,
                                     DBPassword = c.CustomerDBPassword,
                                     DBDataSource = c.CustomerDBDataSource,
                                     DBConnectionString = c.CustomerDBConnectionStringName
                                 }).FirstOrDefault();

                dbcontext.ChangeDatabase(initialCatalog: getDB.DBName, 
                         userId: getDB.DBUserName, 
                         password: getDB.DBPassword, 
                         dataSource: getDB.DBDataSource, 
                         configConnectionStringName: getDB.DBConnectionString);

            return View();
}

在上面的代码中,我只是检查用户输入的子域是否是 macthing,然后更改 EDMX 连接,但现在如何重定向到登录页面客户名称。

客户1/帐户/登录

4

1 回答 1

0

你需要做的是

添加控制器客户

public class CustomerController : Controller
{
    //
    // GET: /Customer/

    public ActionResult Default(string id)
    { 

        if (string.IsNullOrEmpty(id))
            return View("SiteIndex"); //default View for you site like home/index here

        //put your code to check the customer in the DB here
         return View();
    }

}

在 Route.Config 中。在默认路线之前添加客户路线。

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

            routes.MapRoute(
              name: "CustomerDefault",
              url: "{id}",
              defaults: new { controller = "Customer", action = "Default", id = UrlParameter.Optional }
          );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


        }
于 2014-02-14T19:28:04.760 回答