ResolveUrl in external JavaScript file in asp.net project

https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript

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 Displaying Expression 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 ResolveClientUrl() 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 Web Application Root Operator 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 Joel Varty‘s blog):

The following script should place before the external JavaScript reference

<script type="text/javascript">
    var baseUrl = ‘<%= Page.ResolveClientUrl("~/") %>‘;
</script>

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:

原文地址:https://www.cnblogs.com/chucklu/p/9300948.html

时间: 2024-12-10 14:14:57

ResolveUrl in external JavaScript file in asp.net project的相关文章

RequireJS is a JavaScript file and module loader

RequireJS RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJS加载模块化脚本将提高代码的加载速度和质量. /* --- RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in ot

网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)

此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法) http://www.dotblogs.com.tw/mis2000lab/archive/2014/05/10/google_charts-with-asp.net-clientscriptmanager.registerstartupscript.as

Post Complex JavaScript Objects to ASP.NET MVC Controllers

http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/     Post Complex JavaScript Objects to ASP.NET MVC Controllers Posted in ASP.NET'JavaScript August 21, 2009 Use the plug-in postify.js to handle posting comple

AndroidStudio报错:Emulator: I/O warning : failed to load external entity &quot;file:/C:/Users/Administrator/.AndroidStudio3

场景 在进行Android Studio的.Android Studio目录从C盘修改为其他目录后,新建App启动提示: Emulator: I/O warning : failed to load external entity "file:/C:/Users/Administrator/.AndroidStudio3 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 这是

Could not load file or assembly &#39;$SharePoint.Project.AssemblyFullName$&#39;

The fix is simple, do the following: 1.  Open your project file in NotePad 2.  Find the PropertyGroup nodes. 3.  Add a new PropertyGroup node. <PropertyGroup> <TokenReplacementFileExtensions>ashx;</TokenReplacementFileExtensions> </Pr

20 Useful Javascript Frameworks for your Upcoming Project (转)

20 Useful Javascript Frameworks for your Upcoming Project Posted in Resources By ashish On May 14, 2015 Advanced JavaScript programming can often be very difficult and time-consuming to work with. To deal with these difficulties, a lot of JavaScript

javascript file上传并显示图片

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <

C#和JavaScript交互(asp.net前台和后台互调)总结 (转)

http://www.cnblogs.com/poleices/archive/2011/02/24/1963727.html C#代码与javaScript函数的相互调用: 1.如何在JavaScript访问C#函数? 2.如何在JavaScript访问C#变量? 3.如何在C#中访问JavaScript的已有变量? 4.如何在C#中访问JavaScript函数? 问题1答案如下: javaScript函数中执行C#代码中的函数: 方法一:页面和页面类相结合 1.函数声明为public 后台代

JavaScript File API应用——如何设计和实现文件上传组件

(1)精简"带进度条文件上传组件"的设计与实现 XMLHttpRequest第二版为我们提供了便利的progress事件,通过为xhr.upload.onprogress指定处理函数,可以快速制作进度条,JQuery插件参考代码如下: (function($) { $.fn.uploader = function(userOptions) { var options = { id : "uploader", url : "uploadAction.acti