如何在剃须刀中混合服务器端代码

时间:2013-05-21 22:18:27

标签: c# .net visual-studio asp.net-mvc-4 razor

一个微不足道的问题,但解决方案暂时逃脱了我

我们有一个CMS,其中从db!

检索布局和CSS(仅限名称,无扩展名)

.cshtml的代码不能干净地编译

....
 <link href="~/Content/themes/@ViewBag.dbConfig.Theme/style/@ViewBag.dbConfig.CssName.css" rel="stylesheet">    

变量是

  ViewBag.dbConfig.CssName

没有.css扩展名

有没有办法在不改变配置的情况下完成这项工作?

2 个答案:

答案 0 :(得分:1)

首先,我将包装你的变量用法......看看是否会产生更符合你预期的东西。

<link href="~/Content/themes/@(ViewBag.dbConfig.Theme)/style/@(ViewBag.dbConfig.CssName).css" rel="stylesheet">

答案 1 :(得分:0)

使用parantesese和trim:

<link href="~/Content/themes/@(ViewBag.dbConfig.Theme.trim())/style/@(ViewBag.dbConfig.CssName.trim()+".css")" rel="stylesheet"> 
相关问题