Search code examples
jqueryasp.net-mvc-3razorurlhelper

Use a variable that store image path in Razor Url.Content


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..


Solution

  • 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.