Slink Framework - Strongly Typed URLs for ASP.NET

Slink is a code generating Framework that generates type safe URLs for ASP.NET. With Slink URLs, you increase code quality, increase maintainability, and get compile time checking of your URLs in all your ASPX pages (code-behind and non-codebehind).

Introduction

I decided it was time to solve a very annoying and time consuming code maintenance/quality problem when working with ASP.NET and Visual Studio, URL verification. If you're like me, you've probably had your fair share of HTTP 404s and ASP.NET maintenance troubles whenever you renamed a file in your web project and broken URLs in the code-behind pages that contain scattered uses of Response.Redirect() similar to the following:

The site validation in Visual Studio somewhat is limited in its ability to validate URLs. Visual Studio validates URLs in the HTML markup view, but doesn't validate your URLs in code-behind pages. That's a bummer, because the code above is somewhat common in projects. Invalid URLs that exist in your code-behind pages are hard to debug and locate... you usually don't know when you have an invalid URL lurking in your code-behind until you hit the page with your browser. Most modern refactoring tools today do a good job refactoring code for renamed files; however, since most refactoring tools are searching for strings when renaming an ASPX file, there is a potential for error. There have been many times when refactoring didn't help me and only made the problem worse by selecting the "search string literals" option. Refactorability degrades when you start dealing with different media types in your markup and code-behind pages, such as Images, SWF, CSS, and JavaScript URLs. Media types like these go unchecked, try refactoring those in your pages! Personally, I'd love it if there was some kind of strongly typed URL Framework.

Existing Work, PageMethods

PageMethods doesn't generate URLs for pages that do not have the [PageMethod] attribute at the time of this writing.

PageMethods doesn't generate URLs for media types other than ASPX pages. You can't use PageMethods URLs for JavaScript, CSS, SWF or other images.

PageMethods is a bit slow. PageMethods added about 5 seconds to my build time with a project that had about 5 pages.

PageMethods uses BasePage and Invoke(this) in PageLoad. Although, not required, it adds to your page hierarchy.

In summary, PageMethods was too strict, and didn't work well for my purposes. I simply wanted a light-weight and fast URL framework to provide links to pages and media objects that I could use in code-behind pages and HTML markup. I also wanted to know, at compile time, the validity of all the URLs in my project. So, I decided to tackle the problem myself with a small project I created called (Strong Link), aka Slink.

Use as-you-go integration (doesn't require any rewrites of your code to start using Slink)

XML configuration based

Intellisense for any of your files and media objects!

Now that's what I'm talking about. So how do you get your hands on all this Slink ASP.NET goodness? Keep reading...

Getting Started with Slink

There are two basic ways to use Slink:

You can use Visual Studio and an accompanying CR_Slink Plug-In (via DXCore) to automatically generate a type safe file in your projects App_Code directory.

OR

You can use Slink.exe in a build task to generate a strongly typed safe file in your App_Code directory.

Details about how to setup each method is discussed later. First, there's some XML configuration you'll need to perform to tell Slink what to generate. Here is a sample configuration that you place in your Web.config:

This section handler needs to be added to the <configSections>. The <section> tag defines the XML node that is to be handled by the SlinkConfigSection on behalf of the .NET configuration Framework.

<Slink><GlobalSettingsEngineEnabled="True"RootNamespace="Slinks"/>

The <GlobalSettings> node has two attributes EngineEnabled and RootNamespace.

EngineEnabled - Boolean - This is a project level setting. You may want to disable type safe link code generation at the project level if you are working with multiple web projects. Setting EngineEnabled="False" disables code-generation at the web-project level.

RootNamespace - String - Allows you to specify the Namespace for all type safe links.

TypesafeGeneratedFileTarget - String - Allows you to specify the output file where the type safe code should be generated.

NOTE: The Web.config in this example uses the "Web Site Project" model in VS.NET 2005. If you are using the "Web Application Project" model in Visual Studio, you must remove the App_Code from the TypesafeGeneratedFileTarget, place it somewhere other than App_Code. Read step 4 here to understand why you should not put code in App_Code for "Web Application Projects". For more information on the differences between "Web Applications Projects" and "Web Site Projects", click here.

Using Slink.exe

Now, if you're already using the Visual Studio Plug-In, there's no need to use the Slink.exe. But for those of you who don't have Visual Studio, or are using Visual Studio Express, using Slink.exe is the way to go.

Installing Slink.exe

Register the code smith parser COM component that comes with the binary package by running the InstallSlink.bat

Run the following from the command line:

C:\Tools> slink.exe /webproject:<PathToWebProject>

Replace <PathToWebProject> with the full path to your project

Be sure to modify your Web.config and your Namespace rules are set. Otherwise, the Slink won't know what to do.

TIP: Unfortunately, the "Web Site Project" model in Visual Studio are handled differently than ordinary projects. With "Web Site Projects", you can't specify any "Pre-Build" event tasks. But there's an easy workaround for this limitation. If you're using Visual Studio "Web Site Project" model and you want to use the Slink.exe method for generating your type safe file, all you need to do is create a standard dummy "Class Library". Then add a pre-build task for your dummy "Class Library" and setup Slink to execute. Once you add the dummy "Class Library" to your main web-project, your dummy "Class Library" will be built first (and slink will run) before the build / verification process takes place on your main web-project.

That all! After building the web project (if you're using the VS.NET Slink Plug-In) or when you execute Slink.exe, you should get a file named <WebProject>\App_Code\Slinks.cs that contains your type safe URL generated code.

If you have any questions, comments, or suggestions please feel free to contact me. I hope other developers find the Slink Framework useful. Happy coding!

Also, one last note, please use the latest binaries at CodePlex! The binaries here on Code Project may be outdated.

Thoughts & Future Improvements to Slink

Possibly implement /code generate a helper method on media types such as MyImage.GetStream() that would automatically open and return an IO stream.

Licence

This library and code generator is licensed under the terms and conditions defined in the GNU Public License. You are free to use it for non-commercial purposes. If you are going to use any part of it in a commercial application, contact me and we can work something out. You may use the code generated output of Slink in both commercial and non-commercial applications.

Special Thanks

Special thanks and acknowledgements go out to Philip Laureano for building his open source TaHoGen code template generator. Without it, Slinks would not exist.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

I'm a .NET developer with a crazy drive to create neat computer stuff.

You can find me coding the night away in my small garage working on various fun projects for my small startup, Bit Armory, with the hopes of making it big someday, just like the YouTube and MySpace guys did.

When TypesafeGeneratedFileTarget is defined, you are able to define where the type safe code is generated and it won't be generated in the App_Code anymore. Then, simply add WebAppProject\Slink.cs to your project, and you should have your slink genrated code. To have it update, simply add it to your "pre-build" event.

I'll update the plug-in to handle "Web Apps" too. Thanks for bringing this to my attention Let me know if it works for you or if you have any trouble.

In your response.redirect above, you should be using string.format instead of "+". If you use the "+", you are building and destroying multiple strings to create the final string (search Code Project for several articles explaining how string.format and stringbuilder are better at string concatenation).

* PageMethods doesn't generate URLs for pages that do not have the [PageMethod] attribute.
* PageMethods doesn't generate URLs for meda types other than ASPX pages. PageMethods doesn't work for JavaScript, CSS, SWF or other images.
* PageMethods is slow. PageMethods added about 5 seconds to my build time with a project that had about 5 pages.
* PageMethods uses BasePage and Invoke(this) in PageLoad.

I wanted something quick, fast, non-intrusive, and something that had support for media types such as JavaScript files and images.