I want to start serving up my static content from a subdomain in production. What is the best way to do this while maintaining a smooth development experience in Visual Studio? Up until this point, I didn’t have to worry about URLs and I would simply use:
<script src="@Url.Content("~/Scripts/jquery.someScript.js")" type="text/javascript"></script>
When I was local, it would automatically map to http://localhost/myApp/Scripts/jquery.someScript.js and when I went to production, it would automatically map to http://www.myDomain.com/Scripts/jquery.someScript.js. I wouldn’t have to do anything to manage the URLs.
My first instinct would be to use some AppSettings in my web.config and specify HostName and StaticHostName but that would break my usage of Url.Content.
What are some best practices around solving this issue?
Somewhere, you will need to use a configuration setting to indicate which behaviour you require in a given environment (I suppose you could use the IsDebuggingEnabled property but a custom configuration setting is more flexible).
I can think of two possible techniques.
Option 1
You could write your own extension method for
UrlHelperthat picks up the relevant configuration setting. Your view code would then be insulated from knowledge of the configuration, e.g.:Here’s an example implementation (untested):
Option 2
An alternative might be to use something like Combres but modify the configuration per-environment by transforming Combres’ XML configuration file.