Question

Please see my requirement below.

I want to redirect user to account/login page with follwing conditions.

  1. If users types for example http://example.com/Customer1

Where customer1 is customer I am keeping this in one config table where I am having connection string for customer1

so basically customer1 I need to check if this exist in db then redirect to /Customer1/account/login page.

Please let me know if this possible or not? If yes how I can set or check this 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();
}

In above code I am just checking that if subdomain entred by user is macthing then chnage the EDMX connection but now how to redirect to login page customer name.

customer1/Account/Login

Était-ce utile?

La solution

what you need to do is

Add a controller Customer

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();
    }

}

in the Route.Config. add the route for customer before the default one.

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 }
            );


        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top