Reputation: 57
I'm sorry if this is a bit convoluted, but I'm just trying to better understand how Url.RouteUrl() works in a C# .Net environment.
I have an application that deploys to two separate servers via the Publish Web functionality ; one for testing, the other for live production. The live production server is web facing - meaning that it is accessible via a registered domain. The test server is internal and can only be accessed through the network.
One of the functions of this application is to send out an email with a URL that links to a specific page within the app. It obtains this URL using something that looks like:
Url.RouteUrl("Default", new { controller = "foo", action = "bar"}, "http");
This resolves into a URL that is structured with the internal server address regardless of whether it's the testing server or the production server.
What I'd like to be able to do is have it resolve to the internal server address for the testing deployment and the registered domain name for the production deployment, but I can't seem to figure out where those domains are being established.
I have checked the deployment settings (Publish), but they appear to be written correctly for what I would expect.
Ultimately what I'm looking for would be results that look like:
Testing:: http://testserver.internal.domain/foo/bar
Production:: http://www.example.com/foo/bar
Can anyone explain to me where the domain address is being called from in the Url.RouteUrl() method and how it can be changed to suit my needs?
Thanks.
Upvotes: 0
Views: 702
Reputation: 419
The overload you are using is error-prone, I would suggest using this overload.
Save the host in web config file, different for both environments. And pass that in host parameter.
//// overload to use
public virtual string RouteUrl(
string routeName,
RouteValueDictionary routeValues,
string protocol,
string hostName
)
Example usage. 1. Test 2. Production
Url.RouteUrl("Default", new RouteValueDictionary(new { action = "foo", controller = "bar" }),
"http", "test.local.url" );
Url.RouteUrl("Default", new RouteValueDictionary(new { action = "foo", controller = "bar" }),
"http", "production.public.url" );
Upvotes: 1