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.
Filed under: Asp.net, MVC, ASPNETMVC, ASP.NET MVC, Open Source, Telerik