[英]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 从调试器运行时,应用程序的URL看起来像这样
http://localhost:52230/
And in IIS: 在IIS中:
http://localhost/MyApp
Or, when I'm browsing from another PC, 或者,当我从另一台PC浏览时,
http://192.168.168.11/MyApp
Where MyApp = my application's name. 其中MyApp =我的应用程序的名称。
Is there a way, using razor syntax, to get http://localhost
or http://192.168.168.11
only? 有没有办法使用剃刀语法仅获取
http://localhost
或http://192.168.168.11
?
I tried @Url.Content("~/)
but it returns me http://localhost:52230/
我尝试了
@Url.Content("~/)
但是它返回了我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. 当我查看HttpContext.Request标头时,有一个名为“主机”的标头为我提供了Localhost(我通过iis Localhost运行我的主机),所以当你从另一台计算机上运行该主机时,它应该给你Localhost / MyApp我的计算机iis的ip正在运行。
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 否则,在Request.Url中有3个属性可以满足您的需求,即DnssafeHost(由Nilzor指导),授权和主机,我认为最简单的方法是通过
@HttpContext.Current.Request.Url.Host.ToString()
You Just Need to do :: 您只需要做::
var _rootUrl = '@Url.Content("~")';
Use this Function for proper Route independent on root: 将此函数用于独立于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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.