Page.ResolveUrl 在 javascript 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11263425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Page.ResolveUrl is not working in javascript
提问by Thomas
In jQuery code I'm using ResolveUrl("~/DynamicMenu.ashx")
.
在 jQuery 代码中,我使用ResolveUrl("~/DynamicMenu.ashx")
.
But it just returns the exact string:
但它只返回确切的字符串:
"ResolveUrl("~/DynamicMenu.ashx")"
"ResolveUrl("~/DynamicMenu.ashx")"
Here is a bit of code:
这是一些代码:
pageIndex = pageIndex + 1;
var CountryCode = getCookie("SetCountry");
var urlToHandler = '<%= ResolveUrl("~/DynamicMenu.ashx") %>';
urlToHandler = urlToHandler + CountryCode + "&PageIndex=" + pageIndex;
alert(urlToHandler);
What is wrong in my code for ResolveUrl()?
我的ResolveUrl()代码有什么问题?
回答by KyleMit
The problem, as ponchapointed 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 contentfile would, like a .jpg
or .png
.
正如poncha 所指出的,问题在于,就 ASP.NET 而言,您的 .js 文件中提供的内容是一个字符串。它在 IIS 交付之前不应用任何类型的呈现。它得到与任何其他内容文件相同的处理,例如 a.jpg
或.png
.
In order to call server side methods (like ResolveUrl
), you need to use the <% ... %>
syntax within any page that is parsedby ASP.NET (like an .aspx
or .master
file).
为了调用服务器端方法(如ResolveUrl
),您需要<% ... %>
在任何由 ASP.NET解析的页面中使用语法(如.aspx
或.master
文件)。
By the way, these little code blocks go by a lot of different names:
顺便说一下,这些小代码块有很多不同的名字:
In particular, we want a Displaying Expressionwith 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:
知道了这一点,我们可以使用ResolveClientUrl()
which来构建自己的 URL :
returns a URL string suitable for use by the client to access resources on the Web server
返回一个适合客户端使用的 URL 字符串来访问 Web 服务器上的资源
To this, we'll pass in the Web Application Root Operatoror ~
character, where ASP.NET:
为此,我们将传入Web 应用程序根运算符或~
字符,其中 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):
通过组合这些,我们可以通过将以下代码放在您的母版页(改编自Joel Varty 的博客)将显示表达式的结果保存到 JavaScript 变量中:
<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:
由于 JavaScript 变量本质上是全局的,任何其他脚本现在都可以访问该baseUrl
变量,因此我们可以通过以下脚本从 .js 文件中使用它:
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.
现在您可以ResolveUrl("~/DynamicMenu.ashx")
直接从您的 javascript 文件中调用,它将通过去除“~/”并将其替换为服务器端脚本之前创建的 baseUrl 来创建适当的 URL。
Further Reading:
进一步阅读:
回答by Ryan Penfold
Try this solution - ResolveUrl in Javascript
试试这个解决方案 - Javascript 中的 ResolveUrl