【问题标题】:How to mix server side code in razor如何在剃须刀中混合服务器端代码
【发布时间】:2013-05-21 22:18:27
【问题描述】:

一个微不足道的问题,但解决方案现在让我忘记了

我们有一个 CMS,其中的布局和 CSS(仅名称,无扩展名)从 db 中检索!

.cshtml 有这段代码,编译不干净

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

变量在哪里

  ViewBag.dbConfig.CssName

没有 .css 扩展名

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

【问题讨论】:

  • 喜欢javascript的Eval函数?
  • 为什么不在控制器中构建整个路径,然后添加到 ViewBag 中?

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用parantesese和trim:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2014-09-04
      • 2014-03-09
      • 2020-01-25
      • 1970-01-01
      • 2020-02-10
      • 2021-09-28
      相关资源
      最近更新 更多