Reputation: 11360
My application's URL looks like that when I run from debugger
http://localhost:52230/
And in IIS:
http://localhost/MyApp
Or, when I'm browsing from another PC,
http://192.168.168.11/MyApp
Where MyApp = my application's name.
Is there a way, using razor syntax, to get http://localhost
or http://192.168.168.11
only?
I tried @Url.Content("~/)
but it returns me http://localhost:52230/
Upvotes: 1
Views: 2541
Reputation: 199
When I look through the HttpContext.Request headers there is a header called "Host" that gives me Localhost (I run mine through iis Localhost) So for you this should give you Localhost/MyApp, when I run it from another computer this host gives me the ip of the computer iis is running on.
Otherwise in Request.Url are 3 properties that gives me what you need, DnssafeHost (as mentinoed by Nilzor) Authority and Host, I think the easiest way to get it would be through
@HttpContext.Current.Request.Url.Host.ToString()
Upvotes: 0
Reputation: 18603
Try
@(Context.Request.Url.Scheme + "://" + Context.Request.Url.DnsSafeHost)
Upvotes: 2
Reputation: 2307
You Just Need to do ::
var _rootUrl = '@Url.Content("~")';
Use this Function for proper Route independent on root:
function rootUrl(url) {
var _rootUrl = '@Url.Content("~")';
var x = url;
if (url.indexOf(_rootUrl) != 0) {
x = _rootUrl + "/" + url;
x = x.replace(/\/\//g, "/").replace(/\/\//g, "/");
}
return x;
};
use it as::
rootUrl("ControllerName/ActionName");
Upvotes: 0