开发者

ASP.NET MVC Dynamic Stylesheets

The project I'm working on allows an end-user to modify CSS code to integrate the application as best possible. Most of the CSS values are stored in a database and need to be retrieved and parsed dynamically.

I setup a Style controller, and gave each stylesheet an action, and then passed the configuration data through to the View. I set the ContentType to "text/css" and then generated the stylesheets.

This works fine, but the problem I'm running into is this: none of the code works in the application. I include it in the head code, but it doesn't parse in the code.

An example of what I do is this:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" ContentType="text/css" %>

.element {
    background-color: <%= ViewD开发者_如何学Goata.Model.BackgroundColor %>;
}

I include it like so:

<link href="/style/Basic" rel="stylesheet" type="text/css" media="all" />

When I include the CSS code in a partial view and include it using the ViewModel (wrapped in style tags) in an action, then everything works fine. It is when I try to parse this as a separate file when it does not work.

Is there something I am doing incorrectly? Or is there some kind of glitch?

Thanks in advance :D


Use a tool such as HTTPWatch to verify that the stylesheet is being sent down and not 404'd


Controller

 public ActionResult Basic()
    {
        Response.ContentType = "text/css";
        var basicVM = new BasicVM()
                          {
                              BackgroundColor = "Lime",

                          };
        return View(basicVM);

    }

And the View

 <%@ Page Language="C#"   Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Controllers.BasicVM>"       ContentType="text/css" %>
    body {
      background-color: <%= ViewData.Model.BackgroundColor %>;
        }

and the test page

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
     <title>Test</title>
     <link href="/Home/Basic" rel="stylesheet" type="text/css" media="all" />
 </head>
 <body>
     <div>
      Test
     </div>
 </body>
 </html>

Turns everything Green

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜