Download css dynamically in asp.net?

I am creating a content management system in which the user can select the css file from his server, and the application will analyze and store css. An application will need to be able to parse css classes, write them and save css content, which will be dynamically added to another page where the user can select various css classes from the drop-down list. So does anyone know how to add css content to a page dynamically, for example from a database? I found several projects for parsing css here .

Thanks in advance.

+4
source share
3 answers

A good approach for a WebForms project is to link to the .ashx handler on your page instead of a static CSS file:

 <link rel="stylesheet" type="text/css" href="DynamicStyles.ashx" /> 

Then create a handler (add the "Generic Handler" element from Visual Studio), and inside it you can load CSS from the database or anywhere. Just make sure you set the content type in the handler correctly so that browsers recognize the response as a valid stylesheet:

 context.Response.ContentType = "text/css"; 
+5
source

Create a controller that serves CSS content:

 <link rel="stylesheet" href="@Url.Action("GetCss", "Serve", new {id="filename"})" /> 

Controller Code:

 public class ServeController: Controller { public ContentResult GetCss(string id) { string cssBody = GetCssBodyFromDatabase(id); return Content(cssBody, "text/css"); } } 
+9
source

I think NullReference provided you with MVC solution because you checked the message "mvc". If you use ASP.NET web forms, you can use the same method that you use when creating CSS links on the fly on user controls. On the Page_Init event page, do something like the following (in the example below, I am linking to jquery-ui-CSS):

 protected void Page_Init(object sender, EventArgs e) { System.Web.UI.HtmlControls.HtmlLink jqueryUICSS; jqueryUICSS = new System.Web.UI.HtmlControls.HtmlLink(); jqueryUICSS.Href = "styles/jquery-ui-1.8.13.custom.css"); jqueryUICSS.Attributes.Add("rel", "stylesheet"); jqueryUICSS.Attributes.Add("type", "text/css"); Page.Header.Controls.Add(jqueryUICSS); } 

If you want the actual elements to appear in the header, use HtmlGeneric control instead of HtmlLink in my example above. This is still the same method - on the page_Inte, add to the collection Page.Header.Controls:

  protected void Page_Init(object sender, EventArgs e) { System.Web.UI.HtmlControls.HtmlGenericControl mystyles; mystyles = new System.Web.UI.HtmlControls.HtmlGenericControl(); mystyles.TagName = "style"; string sampleCSS = "body { color: Black; } h1 {font-weight: bold;}"; mystyles.InnerText = sampleCSS; Page.Header.Controls.Add(mystyles); } 
+4
source

All Articles