Question

This may be an odd situation but here is my current problem:

I am building an application which is environment driven(ie. Dev/Staging/Live)

So as a user comes to the app for their first time(myapp.local) and I process the default route:

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}", 
            new { controller = "Home", environment = string.Empty, action = "Index", id = UrlParameter.Optional } 
        );

, it sure would be nice to add the environment to the url, so that this:

        myapp.local

becomes this:

        myapp.local/environment/development

Is there a way to do this using C# Asp.Net MVC 2.0?

Thanks.

Was it helpful?

Solution

You could always add route tokens:

routes.MapRoute(
    "Default",
    "environment/{env}/{controller}/{action}/{id}", 
    new { 
        controller = "Home", 
        env = "development", 
        action = "Index", 
        id = UrlParameter.Optional 
    } 
);

This being said adding the environment to your routes is a HORRIBLE idea. I sincerely hope you never do anything like this and that your controller actions would never ever looks anything like this:

public ActionResult Index(string env)
{
    string value = "default";
    if (env == "development")
    {
        value = "some value for dev";
    } 
    else if (env == "staging")
    {
        value = "some value for staging";
    }
    else if (env == "live")
    {
        value = "some value for live";
    }
    else
    {
        throw new Exception("Unknown environment");
    }

    ViewBag.Message = value;
    return View();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top