Obscure but cool feature in ASP.NET 2.0
I learned about a neat little feature that I didn't know existed in ASP.NET 2.0 today that I thought I'd pass on. It is a way to extend the attributes supported by the <%@ Page %> directive at the top of a .aspx page. Previously the supported page directive attributes were hard-coded and the parser only supported the specific ones that ASP.NET knew about out of the box. With ASP.NET 2.0, if you declare a base-class with a public property, you can now set it using a page directive attribute.
To see this in action, save the below class as "MyBase.cs" in your app_code directory:
using System;
public class BasePage : System.Web.UI.Page {
private string message = "blank";
public string Message {
get {
return message;
}
set {
message = value;
}
}
protected override void OnLoad(EventArgs e) {
Response.Write("My Message: " + message);
}
}
and then save the below page as "test.aspx":
<%@ Page Language="C#" message="My Test Message String" Inherits="BasePage" %>
<html>
<body>
</body>
</html>
When you run it you'll get the message "My Test Message String" rendered.
Note: we are using this feature with Atlas which is how I learned about it.