Contents tagged with Tips
-
Solving URL rewriting problems with themes and trailing slashes
In a comment to an old post of mine about URL rewriting, a visitor named Tim has just asked how to solve a problem he was facing with ASP.NET themes and rewriting. The original post was addressing the main problems by using an overload of the RewritePath method introduced by .NET 2. Yet, a simple problem still existed: whenever URL rewriting is used with a URL like ~/somepath/ the theme gets broken because the path to the CSS files and other themed resources (like images) are wrong. The problem here is the trailing slash, which confuses the theme engine. A URL like ~/somepath works fine of course.
-
Library of free data models
The Library of Free Data Models provides "kick-start" database models for free. It can be useful to build examples or quickly start new database schemas.
-
Strongly-typed reflection redux
As a followup to my last post, you can read Daniel Cazzulino's post in which he presents a solution for strongly-typed reflection that complements ayende's approach. Of course this implementation has limitations of its own, the major one being that it's based on lambda expressions and expression trees, two features that won't see the light of day before the next version of C# (sometime in 2007?).
-
Static method reflection
.NET reflection features are powerful and this is really what makes it a powerful platform. We don't use reflection everyday, but when we do it's really useful.
Don't you hate it though when you have to write code like this?:
MethodInfo method = typeof(MyClass).GetMethod("MyMethod");
The problem with this code is that we use a string to identify the method, which means that we don't get compile-time validation.
Ayende has an interesting approach to this problem. He gives you a solution that allows writing the following code instead:
MethodInfo method = GetMethod<int, string>(MyClass.MyMethod);
In this case, everything is strongly-typed and checked by the compiler.
Of course, nothing is perfect and this solution suffers from a number of limitations, but it's an interesting approach anyway. The limitations:- it works only with static methods,
- the methods need to be accessible (public or in your code's reach),
- we can't use a similar approach for properties.
Update: Daniel Cazzulino has another option that is more complete.
-
Copying and pasting text with styles removed - PureText
I've been doing a lot of copy&paste operations between several Word documents or HTML pages lately, and I got fed up with doing "Edit | Paste Special... | Unformatted Text" all the time to avoid having my Word documents polluted with styles I don't want.
A great little free tool came to the rescue: Steve Miller's PureText. I highly recommend that you give it a try!
Pasting text with formatting and styles removed is as easy as using the Apple+V shortcut, an easy replacement for CTRL+V.
Oh, for those who don't know, the Apple key is the one that looks like a window that looks like a waving flag located between CTRL and ALT... -
Using Cache outside web applications
Just because someone asked me how to do it today: here is an article on how to use the System.Web.Caching.Cache object outside of web applications, such as in your Windows Forms applications or console applications.
No need to reinvent the wheel. -
Attach to Visual Studio's Development Web Server easily using a macro
Something I have to do frequently is attaching to a running web application for debugging purposes. I don't always start my web applications in debug mode, but I often need to attach at some point when doing some testing.
Unfortunately, there is no easy way to do this from Visual Studio. We have to attach to the server process "by hand". After some time doing this, you'll find it a bit boring...
Here is a quick-n-dirty solution. It's a simple macro that attaches to the first web server process it finds. Just copy the code below to your macros using the Macro IDE (Tools | Macros | Macro IDE...).
Once you have the macro, you can:- add a button to a toolbar by right-clicking on the toolbars, then "Customize... | Commands | Macros" and drag & drop the command on the toolbar of your choice
- map a shortcut key to it using "Tools | Options | Environment | Keyboard" and search for the AttachToFirstDevWebServer
Warning 2: the code searches for Visual Studio 2005's integrated Development Web Server. To attach to IIS instead (from VS 2003 for example), you'll have to adapt the code to attach to aspnet_wp.exe.
' This subroutine attaches to the first Development Web Server found.
Public Sub AttachToFirstDevWebServer()
Dim process As EnvDTE.Process
For Each process In DTE.Debugger.LocalProcesses
If (Path.GetFileName(process.Name).ToLower() = "webdev.webserver.exe") Then
process.Attach()
Exit Sub
End If
Next
MsgBox("No ASP.NET Development Server found")
End Sub
Update: you may have to replace "webdev.webserver.exe" by "webdev.webserver2.exe" - add a button to a toolbar by right-clicking on the toolbars, then "Customize... | Commands | Macros" and drag & drop the command on the toolbar of your choice
-
Code generation in Visual Studio: build providers, MSBuild tasks, custom tools
PageMethods generates a list of your page methods in an XML file at compile time. This metadata file is then used to generate code you can invoke through the MyPageMethods namespace.
The first version of PageMethods for Visual Studio 2005 supported the only web project model available at that time: the Web Site model. In this model, a custom build provider takes care of the XML file and generates .NET code.
The latest version of PageMethods supports the Web Application project model as well. But a web application being a "class library" - and not strictly a "Web Site" in Visual Studio 2005 terms - build providers cannot be used in this case. Two solutions were possible: create an MSBuild custom task or a Visual Studio custom tool. I chose the latest, as it is easily integrated within Visual Studio and it works with Visual Studio 2002/2003 as well. In fact, custom tools are automatically used for XSD or WSDF files for example.
If you want to learn more about this kind of features, I highly advise you to read Dino Esposito's article about custom build providers, custom MSBuild tasks, and VS custom tools. -
URL rewriting breaks ASP.NET 2's themes
If you try to use URL rewriting and ASP.NET 2 themes at the same time, you will likely get broken pages, because the two don't mix very well.
A typical link to a theme's stylesheet looks like this in the generated HTML:
<link href="App_Themes/Theme1/StyleSheet.css" type="text/css" rel="stylesheet" />
The problem is that these links to CSS documents are using relative paths, which is not going to work fine if you use RewritePath on your web requests. -
Allow strong-named assemblies to be called in shared web hosting environments
One workaround is to set <trust level="full" /> in the web.config file (within system.web element) - although I suspect hosts shouldn't allow this to work.