dynamic Url.Content

空扰寡人 提交于 2020-01-15 12:19:10

问题


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.


回答1:


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" />



回答2:


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" />


来源:https://stackoverflow.com/questions/8263735/dynamic-url-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!