Take the 2-minute tour ×

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:

            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:


becomes this:


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


share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You could always add route tokens:

    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";
        throw new Exception("Unknown environment");

    ViewBag.Message = value;
    return View();
share|improve this answer
Hi, yes that is a great thought, but IIS does not like this, i then get a 403 when i do this –  some_bloody_fool Nov 18 '11 at 21:38
Why is it horrible? –  some_bloody_fool Nov 18 '11 at 21:39
@iwant_B_smrtr, 403 means that you don't have access to the given url. Maybe you misconfigured your web server. I thought it was obvious why this would be a horrible idea. Code like this is impossible to maintain. I would very strongly recommend you having separate virtual directories or even separate servers for your environments. I would also strongly recommend you externalizing all environment specific properties to machine.config or web.config so that your code doesn't look like what I showed in my answer. –  Darin Dimitrov Nov 18 '11 at 21:46
Although your words are hurtful, they are true. –  some_bloody_fool Nov 18 '11 at 22:18
add comment

Your Answer


