博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ResolveUrl in external JavaScript file in asp.net project
阅读量:6945 次
发布时间:2019-06-27

本文共 1633 字,大约阅读时间需要 5 分钟。

The problem, as poncha pointed out, is that as far as ASP.NET is concerned, the content delivered in your .js file is a string. It does not apply any sort of rendering before IIS delivers it. It gets the same treatment any other content file would, like a .jpg or .png.

In order to call server side methods (like ResolveUrl), you need to use the <% ... %> syntax within any page that is parsed by ASP.NET (like an .aspx or .master file).


By the way, these little code blocks go by a lot of different names:


In particular, we want a with the syntax <%= ... %>, where:

the value that is entered after the equals sign is written into the current page

Knowing that, we can build our own own URL by using which:

returns a URL string suitable for use by the client to access resources on the Web server

To this, we'll pass in the or ~ character, where ASP.NET:

resolves the ~ operator to the root of the current application:

By combining these, we can save the result of the displaying expression into a JavaScript variable by placing the following code on your Master Page (adapted from ):

The following script should place before the external JavaScript reference

Since JavaScript variables are inherently global, any other script can now access the baseUrl variable, so we can utilize it from the .js file with the following script:

function ResolveUrl(url) { return url.replace("~/", baseUrl); }

Now you can call ResolveUrl("~/DynamicMenu.ashx") directly from your javascript file and it will create the appropriate URL by stripping out "~/" and replacing it with the baseUrl created earlier by the server side script.


Further Reading:

 

转载地址:http://xpanl.baihongyu.com/

你可能感兴趣的文章
离线用户的灰色头像处理
查看>>
php递归函数return会出现无法正确返回想要值的情况
查看>>
Android Studio之Activity切换动画(三)
查看>>
Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)
查看>>
Redis-Redi事务注意事项
查看>>
ffmpeg mediacodec 硬解初探
查看>>
Cocostudio 1.4 实现的DemoShop
查看>>
Ambari-Blueprint介绍
查看>>
可编辑ztree节点的增删改功能图标控制---已解决
查看>>
Android-自己定义标题栏
查看>>
redis中键空间通知
查看>>
JavaScript快速检测浏览器对CSS3特性的支持情况
查看>>
C#控制台程序输出彩色文字
查看>>
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
查看>>
Python爬虫从入门到放弃(十四)之 Scrapy框架中选择器的用法
查看>>
Yarn架构基本概况(一)
查看>>
使用反射实现 webdriver page 类
查看>>
关于各种无法解析的外部符号问题的相应解决方案【转】
查看>>
COGS 68. [NOIP2005] 采药【01背包复习】
查看>>
安装第三方库出现 Python version 2.7 required, which was not found in the registry
查看>>