[英]How to get base URL of an MVC application using javascript
How do I get the base URL using javascript? 如何使用javascript获取基本URL?
Eg, When I browse my site from visual studio, and if my URL is http://localhost:20201/home/index
, I would want to get http://localhost:20201
例如,当我从visual studio浏览我的网站时,如果我的URL是http://localhost:20201/home/index
,我想得到http://localhost:20201
If I host my site on IIS, and if my virtual directory name is MyApp and the URL is http://localhost/MyApp/home/index
, I would want to get http://localhost/MyApp
如果我在IIS上托管我的站点,并且我的虚拟目录名称是MyApp且URL是http://localhost/MyApp/home/index
,我希望得到http://localhost/MyApp
I tried using location.protocol
+ location.hostname
(and location.host
), they work fine when i browse my site via visual studio, but when I host it on IIS, I get http://localhost
, the /MyApp is truncated off. 我尝试使用location.protocol
+ location.hostname
(和location.host
),当我通过visual studio浏览我的网站时它们正常工作,但当我在IIS上托管它时,我得到http://localhost
,/ MyApp被截断关闭。
You should avoid doing such detection in JavaScript and instead pass the value from the .NET code. 您应该避免在JavaScript中进行此类检测,而是从.NET代码中传递值。 You will always risk running into problems with urls like http://server/MyApp/MyApp/action
where you cannot know which is the name of a controller and which the path to the application. 你总是冒险遇到像http://server/MyApp/MyApp/action
这样的http://server/MyApp/MyApp/action
问题,你不知道哪个是控制器的名称以及应用程序的路径。
In your Layout.cshtml file (or wherever you need it) add the following code: 在Layout.cshtml文件中(或您需要的任何地方)添加以下代码:
<script type="text/javascript">
window.applicationBaseUrl = @Html.Raw(HttpUtility.JavaScriptStringEncode(Url.Content("~/"), true));
alert(window.applicationBaseUrl + "asd.html");
// if you need to include host and port in the url, use this:
window.applicationBaseUrl = @Html.Raw(HttpUtility.JavaScriptStringEncode(
new Uri(
new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)),
Url.Content("~/")
).ToString(), true));
alert(window.applicationBaseUrl + "asd.html");
</script>
The new Uri()
part is needed so that the URL is always combined correctly (without manually checking if each part starts or ends with /
symbol). 需要new Uri()
部分,以便始终正确组合URL(无需手动检查每个部分是以/
符号开头还是结尾)。
var url = window.location.href.split('/');
var baseUrl = url[0] + '//' + url[2];
I don't think that this is possible as the JavaScript (the Client) doesn't know anything about your deployment (MyApp) and treats it as part of the pathinfo (just like /home/index). 我不认为这是可能的,因为JavaScript(客户端)对您的部署(MyApp)一无所知,并将其视为pathinfo的一部分(就像/ home / index一样)。 As a workaround you could try to interpret the pathinfo (location.pathname) according to the domain or port. 作为一种解决方法,您可以尝试根据域或端口解释pathinfo(location.pathname)。
But you could set a JavaScript variable in a global scope (or what ever suits you) containing the path (the path is generated by the server and placed into the variable). 但是您可以在包含路径的全局范围(或任何适合您的范围)中设置JavaScript变量(路径由服务器生成并放入变量中)。
This could look something like that in your html-Head: 这可能看起来像你的html-Head中的那样:
<script type="text/javascript">
var global_baseurl = '<insert server side code that gets the path depending on your server side programming language>';
</script>
Try the below code. 请尝试以下代码。
function getBaseURL() {
var url = location.href; // entire url including querystring - also: window.location.href;
var baseURL = url.substring(0, url.indexOf('/', 14));
if (baseURL.indexOf('http://localhost') != -1) {
// Base Url for localhost
var url = location.href; // window.location.href;
var pathname = location.pathname; // window.location.pathname;
var index1 = url.indexOf(pathname);
var index2 = url.indexOf("/", index1 + 1);
var baseLocalUrl = url.substr(0, index2);
return baseLocalUrl + "/";
}
else {
// Root Url for domain name
return baseURL + "/";
}
}
document.write(getBaseURL());
Thanks, 谢谢,
Siva 湿婆
You can set a base url in the head tag of your page. 您可以在页面的head标记中设置基本URL。 All links/hrefs will use this to call relatieve hrefs. 所有链接/ href将使用它来调用relatieve hrefs。
@{ var baseHref = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Content("~/") }; }
<base href="@baseHref" />
Explanation: 说明:
I think a clean solution will be like 我认为一个干净的解决方案就像
Putting the base URL in the _layout
Html like so 将基本URL放在_layout
Html中就像这样
<div id="BaseUrl" data-baseurl="@Context.Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/")"></div>
Context.Request.Url.GetLeftPart(UriPartial.Authority) will give you in Context.Request.Url.GetLeftPart(UriPartial.Authority)会给你
local http://localhost:20201 from IIS will give you http://localhost 来自IIS的本地http:// localhost:20201将为您提供http:// localhost
Url.Content("~/") in the local will be empty but in IIS will give you the app name, in your case MyApp 本地的Url.Content(“〜/”)将为空,但在IIS中将为您提供应用名称,在您的情况下为MyApp
Then from the Js : 然后从Js :
var baseUrl = $("#BaseUrl").data("baseurl");
Than you can Use it Like: 比你可以使用它喜欢:
$.getJSON(baseUrl + "/Home/GetSomething", function (data) {
//Do something with the data
});
https://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=vs.110).aspx http://api.jquery.com/data/ https://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=vs.110).aspx http://api.jquery.com/data/
var base_url = "@Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/")"
Define it in layout file and append base_url. 在布局文件中定义它并附加base_url。 This worked for me. 这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.