If it is a normal page I can use
public static string GetRootPathJS(Page page)
{
StringBuilder strBuilder = new StringBuilder();
string strApp = page.Request.ApplicationPath;
if (strApp == "/") strApp = "";
strBuilder.Append("<script type='text/javascript'>");
strBuilder.AppendFormat("rootPath=\"" + strApp + "\";");
strBuilder.Append("</script>");
return strBuilder.ToString();
}
In pageonload method just call
ClientScript.RegisterStartupScript(this.GetType(), "root", GetRootPathJS(this));
After this I can call rootPath directly in client side using javascript. But in masterpage code page there is no ClientScript reference. How to get rootpath in masterpage code page. I also tried to get rootpath from children page, however, the javascript in masterpage can not find rootpath defined in children page.
If I understand your question correctly your trying to get the root path in Javascript?
If so you can use the following on the masterpage.master
<script type="text/javascript">
var baseUrl = '<%=Page.ResolveUrl("~")%>';
</script>
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.