如何在MVC中设置MapRoute

时间:2014-02-14 18:37:37

标签: asp.net-mvc asp.net-mvc-routing asp.net-mvc-5.1

请参阅下面的要求。

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

  1. 如果用户输入例如http://example.com/Customer1
  2. 如果customer1是客户,我将其保存在一个配置表中,其中我有customer1的连接字符串

    所以基本上是customer1我需要检查db中是否存在,然后重定向到/ 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连接,但现在如何重定向到登录页面的客户名称。

    customer1表/帐户/登录

1 个答案:

答案 0 :(得分: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 }
            );


        }