ASP.Net MVC:如何在CSS文件中使用razor变量 [英] ASP.Net MVC: How to use razor variable in CSS file

查看:138
本文介绍了ASP.Net MVC:如何在CSS文件中使用razor变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的情况,我需要将此图像的路径/img/product.png 存储在razor变量中,以后我想在CSS文件中使用该razor变量.下面的css代码位于css文件中

as per my scenario i need to store this path /img/product.png of image in razor variable and later i want to use that razor variable in CSS file. below css code is in css file

.test{
    background: url('/img/product.png') no-repeat scroll 0 0 transparent;
}

所以我在下面使用这种方式的代码,但是仍然没有运气

so i use code like this way below but no luck still

.test{
    background: url('@Model.LogoUrl') no-repeat scroll 0 0 transparent;
}

我看到此帖子 http://www .codeproject.com/Articles/171695/Dynamic-CSS-using-Razor-Engine ,但我不想按照该帖子解决我的问题.所以让我知道如何解决这个问题.谢谢

i see this post http://www.codeproject.com/Articles/171695/Dynamic-CSS-using-Razor-Engine but i do not want to solve my issue as per the post. so let me know how to sort this issue. thanks

推荐答案

默认情况下,MVC不支持在外部CSS文件和外部Javascript文件中编写服务器端代码.

By default, MVC does not support writing server side codes in external CSS file and external Javascript files.

为使您的案例生效,您可以在视图文件本身中添加一个内部样式表,它将起作用.

For your case to work, you can add an internal stylesheet in your view file itself, it will work.

例如:

@{
    ViewBag.Title = "Title";
}
<style>
    .test {
        background: url('@Model.LogoUrl') no-repeat scroll 0 0 transparent;
    }  
</style>

这篇关于ASP.Net MVC:如何在CSS文件中使用razor变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆