简体   繁体   中英

Getting URL without port when running from VS, or without application name when running in IIS

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/

尝试

@(Context.Request.Url.Scheme + "://" + Context.Request.Url.DnsSafeHost)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM