71936

dynamic Url.Content

Question:

I am trying to build a website in C# using MVC3 with Razor. I have to build it with themes, but I have problems setting a dynamic content to Url.Content, like it follows:

<img src="@Url.Content("~/Content/themes/THEME-NAME/images/logo.png")" alt="logo" />

and I would like to set the value of THEME-NAME from my code. Let's say from an Application object. Is it possible?

Thanks.

Answer1:

Give the ViewBag a shot.

in your controller action I would put :

ViewBag.ThemeName = "SomeName";

in your view :

<img src="@Url.Content("~/Content/themes/"+ViewBag.ThemeName+"/images/logo.png")" alt="logo" />

Answer2:

Would something like the code show below work for you? did not test it but may lead you in the right direction.

var themName = somevalue var urlValue = "~/Content/themes/" + @themeName + "/images/logo.png" <img src="@Url.Content(@urlValue)" alt="logo" />

Recommend

  • Why are Google search results in UIWebView not triggering webViewDidFinishLoad?
  • Remove 2 vertical borders from HTML table - how?
  • D3 Tree layout visualization - Inherit child with multiple parents
  • ActionBar three-dot dropdown opens at the wrong place
  • Styling text for textarea Wicket
  • Android error - Failed to find style 'toolbarStyle' in current theme
  • How to show underscore (shortcut) without holding Alt?
  • How to modify the HTML tag in Drupal 6?
  • jParallax trouble
  • window.onbeforeunload in javascript
  • File loader changed image file name but not the file name in HTML file
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Python PIL to extract number from image
  • Outputting SharePoint Hyperlink Column as URL
  • Why does it draw lines in the wrong place?
  • Floated image with variable width and heading with background image
  • Disable Kendo Autocomplete
  • How to view images from protected folder with php?
  • Display images in Django
  • How to have background script and something similar to a default popup?
  • HTTP/2 streams vs HTTP/1.1 connections
  • Python urlparse: small issue
  • Syntax for setting draggablecursor property in google maps api
  • Resize panoramic image to fixed size
  • Importing jscolor library in angular 2
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • GridView Sorting works once only
  • How to get icons for entities from eclipse?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal