1

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/

5
  • You should be able to get http://localhost ofter you publish your project, if you set it to be the default website in IIS. Commented Mar 12, 2014 at 8:02
  • But I need something common that will work on both situations Commented Mar 12, 2014 at 8:05
  • Do you want to use it in your code or just see it in the browser? Commented Mar 12, 2014 at 8:06
  • I need a solution that gets me http://localhost whether I run in IIS, or from VS Commented Mar 12, 2014 at 8:07
  • Is this what you are looking for? stackoverflow.com/a/1288383/205859 Commented Mar 12, 2014 at 8:35

3 Answers 3

2

Try

@(Context.Request.Url.Scheme + "://" + Context.Request.Url.DnsSafeHost)
Sign up to request clarification or add additional context in comments.

Comments

0

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");

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.