I’ve been considering the usefulness of adding a CSS management system to my CMS that I’ve been working on. Here are the steps I’ve taken to implement it.
- Create a new MVC applictaion
- Open /Views/Shared/Site.Master
- <link href=”/Css/Site.css” rel=”stylesheet” type=”text/css” />
- This doesn’t point to anything yet
- Open Global.asax
- Add a MapRoute
- routes.MapRoute(“Css”, “Css/{cssFile}.css”, new { controller = “Css”, action = “Index”, cssFile = “Site” });
- Now /Css/Site.css points to Css/Index
- Add a class Css.cs to Models
- Add public string Body { get; set; }
- Add public string Width { get; set; }
- Add public string Background { get; set; }
- Add definitions as desired
- Add a view for CssController/Index
- Make it strongly typed with CSScontrol.Models.Css as the model
- Change contenttype in page directive of Views/Css/Index.aspx to “text/css”
- That should do it.
I’m not sure that I’m convinced of the usefulness of this idea but it’s possible and easy to implement in your ASP.NET MVC project. The possibility of using variables in a CSS file is intriguing.
You may download a sample of the MVC2 project here. Let me know if this is a good idea, a horrible idea or just a bit interesting.
Edit – I’ve added a Response.Filter to this to remove extra spaces, tabs, carriage returns/new lines, final semicolons and comments. It brought the 4.96kb file down to 2.74kb.
Posted in ASP.NET, C#, MVC, Programming by Mark (26Feb10 @ 0946)
Trackback | Permalink | 1 Comment
OK, I named this post that so that perhaps google will pick it up and those new to CSS will find this post.
There is a really good reason that there are no tutorials on the internet concerning how to add content (a site menu is the most common request) to all the pages on your web site using CSS. That’s because CSS doesn’t control content. It controls styling and nothing more. You don’t use styling to add content.
Why not look into some sort of server side includes. PHP includes will handle the situation. ASP.old can handle it but don’t use it unless you have no other choice (and you do). If you’re really fancy and modern and handsome and awesome you can just use ASP.NET MasterPages and/or User Controls.
To wrap it up, CSS doesn’t do that. Stop asking about it! Thank you.
Posted in Programming, Web design, Web standards by Mark (23Feb10 @ 2119)
Trackback | Permalink | Comments Off
Recently someone challenged me to write a server-side method for finding the current season and changing a style sheet accordingly.
The first step is deciding how to differentiate the seasons. I figured to use the day of the year so that astronomically Spring begins on March 21st, the 80th day of the year. Summer begins on the 172nd day, Autumn, the 266th and Winter the 355th. Of course, on a leap year add one day to each, 81, 173, 267 and 356.
Now to find the day of the year use the DayOfYear property. Also, use the IsLeapYear property to take a day away on leap year if the current day is after February 28th (the 59th day of the year). This is easily done by converting the bool IsLeapYear to an Int32 giving a 0 during non-leap years and 1 during leap year.
int doy = DateTime.Now.DayOfYear - Convert.ToInt32((DateTime.IsLeapYear(DateTime.Now.Year)) && DateTime.Now.DayOfYear > 59);
Then find the current season using a few nested ternary operators (I like ternary operators)
string currentSeason = String.Format("{0}.css",((doy < 80 || doy >= 355) ? "winter" : ((doy >= 80 && doy < 172) ? "spring" : ((doy >= 172 && doy < 266) ? "summer" : "fall"))));
I use this in the head of my MasterPage:
<link rel="Stylesheet" runat="server" id="seasonSheet" type="text/css" />
So all I have to do to add the correct style sheet for the current season is this:
seasonSheet.Attributes.Add("href",currentSeason);
Now a stylesheet named spring.css, summer.css, fall.css or winter.css will be added to your rendered MasterPage.
I hope you’ve found this helpful.
Posted in ASP.NET, C# by Mark (28Dec09 @ 1518)
Trackback | Permalink | No Comments
While reading about a recent question concerning @@Identity and the reasons to use SCOPE_IDENTITY() instead I experimented with LINQ to get the SCOPE_IDENTITY() results.
using (var dbc = new siteDataContext())
{
dbc.Log = Console.Out;
repertoire newSkill = new repertoire
{
skill = "a new item"
};
dbc.repertoires.InsertOnSubmit(newSkill);
dbc.SubmitChanges();
Console.Write(newSkill.id.ToString());
}
It seems that calling the id column of the inserted object gives you the SCOPE_IDENTITY(). Here is the SQL Log:
INSERT INTO [dbo].[repertoire]([skill])
VALUES (@p0)
SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input NVarChar (Size = 10; Prec = 0; Scale = 0) [a new item]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
Posted in ASP.NET, C#, LINQ, Programming by Mark (31Oct09 @ 1058)
Trackback | Permalink | No Comments
WebAIM’s Screen Reader Survey, conducted December 2008 to January 2009, is a very interesting read. Yet another nail in the Flash coffin but surprisingly Frames are deemed easy to use by nearly 60% of screen-reader users.
Give it a quick look. Comments open.
Posted in Web design, Web standards by Mark (10Feb09 @ 2033)
Trackback | Permalink | No Comments
I was working on a project and just on a whim decided to try out some of .NET’s AJAX controls. I chose a random page, added a ScriptManager, tossed in a TextBox and a CalendarExtender.
F5. Page shows up. Javascript error:
Line: 121
Char: 1
Error: 'Sys' is undefined
Code: 0
Url: http://localhost:49329/admin.aspx
Hmm.
Try a new project. Add a ScriptManager, tossed in a TextBox and a CalendarExtender. Works fine. Huh?
I go through the Web.Config files of the two. Nothings changed except the version number on everything:
non working: Version=3.5.0.0
working: Version=1.0.61025.0
Ah ha! That must be it. I reset the working project as a 3.5 app and run it. I was so sure it was going to fail. Nope. Worked fine.
Must be something else. *grumble*
I start taking pieces out of my existing app and when I came to the Global.asax I took out the RouteTables and AJAX comes to life! FINALLY!
Here is what I had:
Global.asax
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
private static void RegisterRoutes(ICollection<RouteBase> Routes)
{
RouteTable.Routes.Add(
new Route("{Parameter}/{pageID}", new RouteHandler()));
RouteTable.Routes.Add(
new Route("{Parameter}", new RouteHandler()));
}
With my routing handler file showing this little gem:
RouteHandler.cs
string virtualPath = string.Format("~/{0}.aspx", pageName);
return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));
So basically it was swiping the file extensions from my httpHandlers and replacing them with .aspx meaning they were not found.
Web.Config
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>
To handle the situation I added the following to Global.asax:
Global.asax
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
private static void RegisterRoutes(ICollection<RouteBase> Routes)
{
RouteTable.Routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}",
new System.Web.Routing.StopRoutingHandler()));
RouteTable.Routes.Add(new System.Web.Routing.Route("{service}.asmx/{*path}",
new System.Web.Routing.StopRoutingHandler()));
RouteTable.Routes.Add(
new Route("{Parameter}/{pageID}", new RouteHandler()));
RouteTable.Routes.Add(
new Route("{Parameter}", new RouteHandler()));
}
I hope this helps.
Posted in ASP.NET, C#, Programming by Mark (30Jan09 @ 1614)
Trackback | Permalink | Comments Off