使用在 Razor Url.Content 中存储图像路径的变量
我使用一个函数来动态构建图像路径,如下所示:
function () {
var name = $(this).attr('id') + '_o';
var newsrc = '../../Content/HomePage/' + name + '.png';
我想使用 Razor Url.Content 将 newsrc 分配给图像标签作为其源。所以,我希望做这样的事情:
$(this).attr('src', '@Url.Content(newsrc)');
但是VS向我显示红线,表示newsrc在当前上下文中不存在。 如何将变量与 Url.Content 一起使用?
(注意:我正在使用这个内部脚本标签...)
编辑:我想到了一个想法,也许后门会将 newsrc 传递到控制器,在那里使用 Url.Content 来解析 url 并将其传递回来?可行吗?
任何帮助将不胜感激..
I used a function to construct an image path dynamically like this:
function () {
var name = $(this).attr('id') + '_o';
var newsrc = '../../Content/HomePage/' + name + '.png';
And I want to assign the newsrc using Razor Url.Content to an image tag as its source. SO, I wish to do something like this:
$(this).attr('src', '@Url.Content(newsrc)');
But VS show me red line saying that the newsrc is not exist within current context..
How can I use a variable along with Url.Content?
(Note: I am using this inside script tag...)
EDIT: An idea comes to my mind, perhaps the backdoor will be passing the newsrc to controller, use Url.Content there to resolve the url and pass it back? Is it feasible?
Any help will be appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我使用这种技术将路径发送到 JavaScript 变量时,它会显示 -1 而不是路径。代码如下。
When I am using this technique to send the path to JavaScript variable then it shows me -1 instead of path. The code is as below.
您正在尝试直接将 javascript 变量传递给 Asp.Net 代码 - 这是不可能的。
您必须删除
@Url.Content()
并仅使用 javascript 中的相对路径:或者,使用
Url.Content()
获取文件夹位置,并在 javascript 中附加文件名,如下所示:You are attempting to directly pass a javascript variable to Asp.Net code - which is impossible.
You will have to remove the
@Url.Content()
and just use the relative path in your javascript:Or alternatively, use the
Url.Content()
to get the folder location, and append the filename in javascript, like this:问题被我的前辈解决了。我在 web.config (WebDirectory)中创建一个额外的 appsettings,然后将值指定为文件夹路径。
然后,在我的代码中,我可以使用以下方法轻松构建我的网址:
只需在此处分享我的解决方案,希望它能帮助将来的人。谢谢。
Problem solved by my senior. I create an extra appsettings in web.config ( a WebDirectory) then assign the value as the folder path.
Then, at my code, I can easily construct my url using:
Just share my solution here, hope it will help someone in future. Thanks.