4

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;
}

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

4
  • 1
    Better to use internal style sheet in view itself. Commented Jul 28, 2016 at 11:15
  • @ssilas777 sorry do not understand what u try to say......can u plzz come wit some example to describe what u said. thanks Commented Jul 28, 2016 at 11:18
  • is this code on your .cshtml file or .css file? Commented Jul 28, 2016 at 12:07
  • it is possible, and it's already been answered here: stackoverflow.com/questions/4492748/dynamic-css-for-asp-net-mvc Commented Feb 7, 2018 at 13:51

3 Answers 3

5

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.

Eg:

@{
    ViewBag.Title = "Title";
}
<style>
    .test {
        background: url('@Model.LogoUrl') no-repeat scroll 0 0 transparent;
    }  
</style>
Sign up to request clarification or add additional context in comments.

7 Comments

It is similar to same question
No he is trying to put in external css file, I want him to put in view itself.
background: url('@url.content(Model.LogoUrl)'); may help :)
how to return view which will have only css with razor variable ? looking for a similar article on this subject. please discuss how to achieve it. thanks
I didn't get your question clearly.. Can you post it as a seperate question.. I'll surely try to help
|
0

As far as I know, you cannot refer or write Razor code in a CSS file. CSS files are processed by the browser where as the Razor code is executed by the server and then rendered by the browser

For your problem, you will need to write an inline style

1 Comment

0

You can also do it in HTML part like this:

For example:

<div class="test" style="background: url([email protected]) no-repeat scroll 0 0 transparent;">
...
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.