Roundup of ASP.NET Dynamic Data

June 1, 2009

Dynamic Data is a very nice tool comes with .NET 3.5 SP1. It offers CRUD support out of box (scaffolding) and is also very easy to customize!

Add to an existing web applicataion: for many web applications (especially those existing LOB apps), using Dynamic Data on backend admin pages is probably the most common use of it. MSDN has an article here to cover the steps to add Dynamic Data to your existing ASP.NET app.  It worked perfected for me. This article also tells you how to add Dynamic Data to a sub-folder.

Another trick on securing the \DynamicData folder is to add <authorization> policies in web.config file for\DynamicData folder.

http://www.asp.net has a good video series about Dynamic Data. Below are my roundups of the main knowledge points, and the things I learned after playing with it:

  • URL routing: “List|Details|Edit|Insert” and ListDetails.aspx can co-exist. No need to comment one out.  Enabling both of them allows the app to support both ways ( separate page editing or in-place editing) – all determined by the way you request the page.  Actually I think this is better.
  • CustomPages: just create a folder under it named after the plural format of the table name (Products).
  • ScaffoldAllTables = false: for security reasons this should be turned off.  However for any table you want to scaffold you have to do one of these 2 things
    • extend the table class and decorate it with [ScaffoldTable(true)]
    • create custom pages in \CustomPages folder
  • Validation:
    • Declarative: [Required] [Range(0, 2000)]
    • Imperative: extend those partial OnXXX event handlers, and throw Exceptions with error message.
  • Hide a column from scaffolding: [ScaffoldColumn(false)]
  • Change display: [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
  • Customized edit control: [UIHint("ReadOnly")], then in \FieldTemplates folder, add a ReadOnly_Edit.ascx

Mysterious Double Post Back/Page Request

March 10, 2009

Recently I run into a strange situation that one of my pages was always loaded twice for each reqeust. This happened only when I enable  “URL Rewrite” feature for the site.  After almost a hour of debugging, I found it’s caused by a line in stylesheet like this:

src=images/mypic.gif

In a regular, non-URLRewrite case, the broswer will correctly render the relative location of this image from the web page, which is at the root folder. It will resolve the location of mypic.gif to be /images/mypic.gif.

However, when URL rewrite was turned on, my page’s URL changed from /JobDetails.aspx?id=123 to /Jobs/Orange County/IT/123/Web Designer Needed. For the broswer, the page is located at many levels deep from the root. As the result, the path of above image will be resolved to /Jobs/Orange+County/IT/123/Web+Designer Needed/images/mypic.gif.

The retrieval of the image is a separate GET HTTP request from the broswer. Unfortunately, the URL of the image happens to match one of the URL rewrite rules. The result: this image request gets to translated to /JobDetails.aspx?id=123 and this ASPX page is requested twice!

Lesson: always use absolute path, espacially when URL rewrite is used.


Follow

Get every new post delivered to your Inbox.