Thursday, August 27, 2009 9:03 PM Kazi Manzur Rashid

Convention over Configuration and Telerik Extensions for ASP.NET MVC

Convention over Configuration is getting more and more popular in the .NET space, especially after the release of ASP.NET MVC. In our extensions, there are quite a few places where you will find this concept, lets consider the following snippet of ScriptRegistrar:

<% Html.Telerik().ScriptRegistrar()
                 .Scripts(script => script.AddGroup(
                                                        "validation",
                                                        group => group.Add("jquery.validate.js")
                                                                      .Add("xVal.jquery.validate.js")
                                                                      .Combined(true)
                                                   )
						 )
				 .Render(); %>

Check that we did not mention any path for the script files, by default it assumes that all the script files are located in the default “Scripts” folder that is created when you create a ASP.NET MVC Application in Visual Studio. Now what if the script files resides in a different folder.

<% Html.Telerik().ScriptRegistrar()
                 .Scripts(script => script.AddGroup(
                                                        "validation",
                                                        group => group
                                                                      .DefaultPath("~/Scripts/Validation") // Setting the default path of a Group.
                                                                      .Add("jquery.validate.js")
                                                                      .Add("xVal.jquery.validate.js")
                                                                      .Combined(true)
                                                   )
						 )
				 .Render(); %>

Now what if one of the file in a group resides in a different folder:

<% Html.Telerik().ScriptRegistrar()
                 .Scripts(script => script.AddGroup(
                                                        "validation",
                                                        group => group
                                                                      .DefaultPath("~/Scripts/Validation")
                                                                      .Add("jquery.validate.js")
                                                                      .Add("~/SomeOtherPath/xVal.jquery.validate.js") // Check that we are setting the complete path.
                                                                      .Combined(true)
                                                   )
						 )
				 .Render(); %>

Okay, now what if you want to change the default location of both scripts and stylesheet files, Just add the following lines in your application start (global.asax maybe):

WebAssetDefaultSettings.StyleSheetFilesPath = "~/assets/stylesheets";
WebAssetDefaultSettings.ScriptFilesPath = "~/assets/scripts";

So by convention we are assuming the default locations (for script “~/Scripts” and Stylesheet “~/Content”) but with configuration (the above 2, 3, 4 code snippets) you can completely override it. Let me show one more case before completing this post.

We all know we should minify our script and stylesheet files with tools like Yahoo compressor before making the application live. Lets say we have minified the above two files with a minifying tool and saved in the Scripts directory with .min.js extension. Now, if you run the application in release mode debug = “false” in web.config, you will find that the ScriptRegistrar is picking up the .min.js instead of .js or debug.js and the vice versa when debug=”true” and the same holds true for StyleSheetRegistrar.

Shout it
Filed under: , , , , ,

Comments

# Convention over Configuration and Telerik Extensions for ASP.NET MVC - Kazi Manzur Rashid's Blog

Thursday, August 27, 2009 10:27 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Convention over Configuration and Telerik Extensions for ASP.NET MVC - Kazi Manzur Rashid's Blog

Thursday, August 27, 2009 11:36 AM by progg.ru

Thank you for submitting this cool story - Trackback from progg.ru

# re: Convention over Configuration and Telerik Extensions for ASP.NET MVC

Friday, August 28, 2009 11:58 AM by Trevor

Nice info.  Just one thought.  Don't use tabs in your source code and set your indentation to something like 2 or 4.  It's very hard to read scrolled way over like that.

# Twitter Trackbacks for Convention over Configuration and Telerik Extensions for ASP.NET MVC - Kazi Manzur Rashid's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Convention over Configuration and Telerik Extensions for ASP.NET MVC - Kazi Manzur Rashid's Blog         [asp.net]        on Topsy.com

# ASP.NET MVC Archived Blog Posts, Page 1

Monday, August 31, 2009 1:41 AM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1