Click here to Skip to main content
14,105,616 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 6 Mar 2012
Licenced CPOL

Cleaning Up That Ugly Client-side ASPX Code

, 6 Mar 2012
Rate this:
Please Sign up or sign in to vote.
An easy way to clean up client-side ASPX code

When managing a big web project, you might find yourself ending up with a big master page, that has a bunch of centralized code client side logic, like includes. What I mean by 'includes', is the part of the code where you spit out all the declarations for JS and CSS files. I got to this stage many times by myself, and I've seen it happen at my various work places as well.

This usually ends up to some big nasty chunk of code that looks like this (and this is a relatively small example of what I mean...):

<link rel="stylesheet" href="../../Content/bootstrap.css" />
<link rel="stylesheet" href="../../Content/common.css" />
<link rel="stylesheet" href="../../Content/widgets.css" />
<script src="" 

<script src="@Url.Content("~/Scripts/bootstrap/bootstrap-dropdown.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/common.js")" 

<script src="@Url.Content("~/Scripts/homepage.js")" 

<script src="@Url.Content("~/Scripts/add-form.js")" 

<script src="@Url.Content("~/Scripts/search-form.js")" 

<script src="@Url.Content("~/Scripts/bookmark-list.js")" 

<script src="@Url.Content("~/Scripts/register.js")" 

<script src="@Url.Content("~/Scripts/openid.js")" 


Why Is This Bad?

For starters, there's so much similar text here, that typing it all up should've rung a huge bell for us, screaming "THIS IS WRONG!!". The clutter of text here bothers us from seeing what's really important here, and all that is, is the files being included here.

Why Does This Happen?

The main reason for this is because we don't think of client side code as 'actual' code. This leads to us forgetting about DRYing up our code. The equivalent of this in server side code would be to copy the whole body of a certain method a dozen times, and just changing one parameter between different versions. If this were server code, we would've easily spotted the code duplication, and extracted this to a method that receives a parameter and saved a lot of typing, and kept the maintainability of the code.

How Do We Fix This?

All we need to do is introduce a small helper method that will print this again and again for us. We can do this on the client and on the server. In this case, I would go with doing this on the client, just because this piece of code will probably only be useful for us in this specific context.

Here is my clean solution to this problem:

@{ Func<string, string> 
JsRequire = s => "<script src=\"" + Url.Content(s) + 
"\" type=\"text/javascript\"></script>"; }
@{ Func<string, string> CssRequire = s => 
"<script rel=\"stylesheet\" href=\"" + s + "\" />"; }




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gilly Barr
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# &, while serving in the IDF.
Worked on the 'Core Performance' Team at (Sears Israel)
Currently working at

Check out my blog!
or my twitter

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Jimzie from Fresno12-Mar-12 9:25
memberJimzie from Fresno12-Mar-12 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190518.1 | Last Updated 6 Mar 2012
Article Copyright 2012 by Gilly Barr
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid