Adam
Adam

Reputation: 459

MVC Route Mapping

I have a website, xyz.com and users are setting up admin accounts where they then have other users that are registering under them. They call their account name wisconsinsponsor. Another user sets up another account called iowasponsor. So I want to be able to have the ability that a user could browse to xyz.com/wisconsinsponsor and xyz.com/iowasponsor and get funneled into the appropriate settings that these users have setup.

So then after I browse to xyz.com/wisconsinsponsor which will allow me to get the appropriate settings for wisconsinsponsor I can be dropped onto xyz.com/wisconsinsponsor/{controller}/{method}.

So I added the following code.

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

    List<Sponsor> sponsors = new SponsorContext().Sponsors.ToList();

    foreach (Sponsor sponsor in sponsors)
    {
        // ALL THE PROPERTIES:
        // rentalProperties/
        routes.MapRoute(
            name: sponsor.SponsorName,
            url: sponsor.SponsorName + "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = sponsor.SponsorId
            }
        );
    }


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

So the main goal is that without logging in, I can get information that pertains to each "sponsor" and then just generic information if a user goes to 'xyz.com' without specifying a sponsor. The below works to a point for landing on the home page, but then when I navigate to login or any other view, I get for example 'xyz.com/[my first sponsor entry in the database]/admin/login' instead of 'xyz.com/admin/login'. Why doesn't the navigation fall to the Default route?

Upvotes: 0

Views: 121

Answers (1)

Anthony Shaw
Anthony Shaw

Reputation: 8166

Change your route to simply include the sponsor, don't create individual routes for every sponsor, this is where routing becomes so powerful

routes.MapRoute(
    name: null, //routes names must be unique, but you can have multiple named null (go figure)
    url: "{sponsor}/{controller}/{action}/{id}",
    defaults: new
    {
        sponsor = "defaultSponsor", //or whatever you want the default to be
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);

Then, decorate each of your action methods with the sponsor argument

public ActionResult Index(string sponsor, int id) { }

Rereading your question tho, this doesn't help in the instance of not having a sponsor, unless your "defaultSponsor" is not really a sponsor, but your generic information that is presented. So when no sponsor is passed in the address bar to the routing, you see 'defaultSponsor' or and empty string and could then handle appropriately

Another reason to handle it this way, is RegisterRoutes is only called upon application start up, so if they were dynamically added while the app is running, they would be invalid routes until the application is restarted. By making it an argument, they would work dynamically as well.

Upvotes: 2

Related Questions