使用在 Razor Url.Content 中存储图像路径的变量

发布于 2024-12-20 02:31:12 字数 540 浏览 2 评论 0原文

我使用一个函数来动态构建图像路径,如下所示:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁〃客ぐ 2024-12-27 02:31:12

当我使用这种技术将路径发送到 JavaScript 变量时,它会显示 -1 而不是路径。代码如下。

var path= <%= ConfigurationManager.AppSettings["FileManager"] %>;

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.

var path= <%= ConfigurationManager.AppSettings["FileManager"] %>;
物价感观 2024-12-27 02:31:12

您正在尝试直接将 javascript 变量传递给 Asp.Net 代码 - 这是不可能的。

您必须删除 @Url.Content() 并仅使用 javascript 中的相对路径:

$(this).attr('src', newsrc);

或者,使用 Url.Content() 获取文件夹位置,并在 javascript 中附加文件名,如下所示:

var name = $(this).attr('id') + '_o';
var newsrcFolder = '@Url.Content('../../Content/HomePage/')';
var newsrc = newsrcFolder + name + '.png';
$(this).attr('src', newsrc);

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:

$(this).attr('src', newsrc);

Or alternatively, use the Url.Content() to get the folder location, and append the filename in javascript, like this:

var name = $(this).attr('id') + '_o';
var newsrcFolder = '@Url.Content('../../Content/HomePage/')';
var newsrc = newsrcFolder + name + '.png';
$(this).attr('src', newsrc);
我的黑色迷你裙 2024-12-27 02:31:12

问题被我的前辈解决了。我在 web.config (WebDirectory)中创建一个额外的 appsettings,然后将值指定为文件夹路径。

然后,在我的代码中,我可以使用以下方法轻松构建我的网址:

 System.Configuration.ConfigurationManager.AppSettings["WebDirectory"].ToString()...

只需在此处分享我的解决方案,希望它能帮助将来的人。谢谢。

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:

 System.Configuration.ConfigurationManager.AppSettings["WebDirectory"].ToString()...

Just share my solution here, hope it will help someone in future. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文