2

I want some flexibility in my css style block so i decide to inject some server side code, f.e.

#defaultGameContainer {
            position: relative;
            width: @(SiteConfig.Instance.Game.Width + "px");
            height: 600px;
            top: 100px;
            left: 50%;
            margin-left: -480px;
        }

But it seems not work, i have a error in VS like 'unexpected characters sequence...' Are we restricted to use Razor syntax in css? Thanks.

4 Answers 4

5

Actually, you can add a custom CSS block for a specific .cshtml View by following these instructions:

First off, Define in the .cshtml view your custom CSS block

@section CustomCSS{ 
<style>
    #floorsList > tbody > tr > td {
        vertical-align: middle
    }
</style>
}

Then, you need to embed this CustomCSS section in _layout.cshtml file:

<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Your Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @RenderSection("Styles", false)
    <!--Consider the following line -->
    @RenderSection("CustomCSS", required: false)
 </head>
 <body>
 </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

This is limited to layouts
3

css/js files are not parsed by mvc razor views engine

you can create CssController (be sure you don't have Css folder in project directory) which returns plain text

controller:

public class CssController : Controller
{
    [OutputCache(Duration = 6000, VaryByParam = "l")]
    public ActionResult generatecss()
    {
        return View("generatecss");
    }
}

view: generatecss.cshtml

@{ Layout = ""; }
#defaultGameContainer {
        position: relative;
        width: @(SiteConfig.Instance.Game.Width + "px");
        height: 600px;
        top: 100px;
        left: 50%;
        margin-left: -480px;
    }

put it in layout as stylesheet

<link href="~/Css/generatecss" rel="stylesheet"/>

1 Comment

Hi your need to return the correct type/mime type in the controller
1

I believe you can use LESS Css for .NET http://www.dotlesscss.org/

4 Comments

Thank you for response. I think it is little overhead to linking entire library to achieve so simple task, because i have only simple css block that not intended to grow later.
@igorGIS humm.. then you can use ajax to retrive "SiteConfig.Instance.Game.Width" and assign it using jquery to your #defaultGameContainer: $("#defaultGameContainer").css("width", ajaxResult)
Sounds like a solution. Very strange, as i remember we can use <%= %> syntax in asp.net 4 in any part of page, including css bloks. Why we can't in MVC
because CSS are not registered type to MVC's Razor engine, only cshtml are recognized. So you can only put css block into <style> tag within a cshtml page, this can work. But seperate .css file won't be processed by razor.
0

Thanks you guys! All you suggest is just fine! And at the end of experimenting i found that it works even with those annoying VS warnings. So we can just mix c# with css just in <style>...</style> block in main _layout.cshtml and ingore those warnings.

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.