Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Dev Blog - Johan Danforth

I'm Johan Danforth and this is my dev blog - a mix of .NET, ASP.NET, Rest, Azure and some other random coding stuff.

  • Windows Defender

    If you don't have it installed yet you should consider it. I have been running the Anti Spyware beta from Microsoft for quite some time now, and today it automatically updated itself to Windows Defender Beta 2. This program has been quite helpful with protecting my kids' computers. The young ones doesn't always know what is harmful to their boxes or not. I've told them "if you see a big pupup with a spyware warning in the right corner - let me know at once" :)

    About Windows Defender:

    Windows Defender (Beta 2) is a free program that helps protect your computer against pop-ups, slow performance, and security threats caused by spyware and other unwanted software. It features Real-Time Protection, a monitoring system that recommends actions against spyware when it's detected, and a new streamlined interface that minimizes interruptions and helps you stay productive.

  • [.NET 2.0] Visual Studio 2005 Web Application Project - Old Skool

    My thanks to ScottGu who commented on my earlier blog post about checking out the Visual Studio 2005 Web Application Project, which gives you Old Skool style of working with ASP.NET applications. I'm sure lots of people (like me) prefer the way it used to work. Whole thing with teaching old dogs... Snip from the site:

    The Visual Studio 2005 Web Application Project Model is a new web project option for Visual Studio 2005 that provides the same conceptual web project approach as VS 2003 (a project file based structure where all code in the project is compiled into a single assembly) but with all the new features of VS 2005 (refactoring, class diagrams, test development, generics, etc) and ASP.NET 2.0 (master pages, data controls, membership/login, role management, Web Parts, personalization, site navigation, themes, etc).

  • [.NET 2.0] Using Web Deployment Projects with Visual Studio 2005

    Got a tip from a friend to have a look at this page on the MSDN webby about different web deployment options with VS.NET 2005. Snippet from that page:

    Visual Studio 2005 provides deployment support though its Copy Web Site and Publish Web Site features. While these are ideal for many scenarios, there are other, more advanced scenarios where developers need the following capabilities:

      • More control over assembly naming and output.
      • Custom pre-processing and post-processing for the build.
      • The ability to exclude, add, and transform files and directories during builds.
      • The ability to modify the Web.config file to change database connection strings, application settings, or the URLs for Web references, depending on the build configuration. (For example, it might be necessary to use different values for development, test, staging, and release settings).
    This white paper describes a solution to these advanced scenarios and introduces a new feature called Web Deployment Projects for Visual Studio 2005.

    There is a VS.NET plugin available on that page with the following features for building ASP.NET 2.0 web sites:

    • ASP.NET 2.0 precompilation as part of the build process.
    • More flexible options for generating compiled assemblies from a Web project, including these alternatives:
      • A single assembly for the entire Web site.
      • One assembly per content folder.
      • A single assembly for all UI components.
      • An assembly for each compiled file in the Web site.
    • Assembly signing options.
    • The ability to define custom pre-build and post-build actions.
    • The ability to exclude folders from the build.
    • The ability to modify settings in the Web.config file, such as the <connectionString> element, based on the Visual Studio build configuration.
    • Support for creating .msi files with setup projects.

    The extensibility of Web Deployment projects enables you to tailor the build and deploy process to suit your needs. This is done without sacrificing the optimized workflow improvements achieved with Visual Studio 2005 Web site projects.

    I still have to read the whole page myself and try out the add-in. Looks interesting and useful though.
  • [.NET 2.0] Writing Your Own Provider

    Being intrigued by the provider system, I though I would spend some time on the excellent Provider Toolkit page on MSDN to see if I could understand how to write your own provider. From scratch that is, not based on the membership stuff.

    I found this great page, which describes how to write an SqlImageProvider from ground up. The sample code on the page got some errors in it, but I got it working eventually, and it's just amazing. I'll write another really simple provider and post the code here soon.

    I'm thinking what I could use the provider system for... perhaps if you wanted to construct a system where you know you would start using Access or even XML files, then at a later state move to SQL or Oracle. You could write the whole interface to your DAL as a provider... perhaps a bit too much code to be funny, but it could work. Each method would have to be written at least three times - once for the base, once for the service and of course once for the concrete provider where you would probably use whatever helper classes you got to actually manage the data. Oh well...

  • [.NET 2.0] Taking a Web App Offline

    This is a neat feature of ASP.NET 2.0 I just found out about. Loads of people have already written about it, but it's so useful that I decided to put it on my blog as well. By placing a file called 'app_offline.htm' in the root directory of your web app, no ASP.NET dynamic pages are no longer served. The web server returns the content of that file instead. This is what ScottGu wrote about this feature on his blog:

    Basically, if you place a file with this name in the root of a web application directory, ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application.  ASP.NET will also then respond to all requests for dynamic pages in the application by sending back the content of the app_offline.htm file (for example: you might want to have a “site under construction” or “down for maintenance” message).

    This provides a convenient way to take down your application while you are making big changes or copying in lots of new page functionality (and you want to avoid the annoying problem of people hitting and activating your site in the middle of a content update).  It can also be a useful way to immediately unlock and unload a SQL Express or Access database whose .mdf or .mdb data files are residing in the /app_data directory.

    Once you remove the app_offline.htm file, the next request into the application will cause ASP.NET to load the application and app-domain again, and life will continue along as normal.

    You may want to read all the comments to this blog entry, some interesting conversation there.

    I just happened to stumble upon this feature when I was publishing my web app directly from within VisualStudio (Build->Publish...), because VS automatically places that file in the web app during upload of the new files. Neat. What is not so neat is that VS didn't remove the app_offline.htm file once it was done publishing :) Maybe it was because I was publishing to an FTP site... not sure. Will try again some other time.

  • [Books] Professional ASP.NET 2.0? Naaaah...

    Every now and then I like to read literature related to coding, design, refactoring and various development methodologies, especially when I know there's something new out there that I haven't been able to work with or look at yet.

    This time I thought I'll get myself a really fat and heavy book on ASP.NET 2.0 which digs into the changes between 1.1 and 2.0 and some tips on how to best use the goodies like new controls, membership, web parts and so on. Without doing too much investigation, I used the Internet book-shop I usually use and did a search on "ASP.NET 2.0". I got a number of hits and selected "Professional ASP.NET 2.0" from Wrox. The book is supposed to help "experienced developers make the transition to ASP.NET 2.0".

    I should have read some reviews I guess... it was kind of a bad buy it seems. 1258 pages (!) and the book doesn't even bring up how to hide menu items based on a web.sitemap file by editing the sitemap provider section in web.config. Something this great video on the MS ASP.NET Developer Center shows. One reason the book is so thick is probably because each code sample is in both VB.NET and C# and you also get page after page with class properties. I dunno, but Wrox got a separate ASP.NET 2.0 book on security, roles and stuff, maybe they brought it up there. I may have had bad luck with the sitemap provider thing I was looking for, but there were other things I needed that were missing as well. I can't say I've looked at ALL thousand or more pages, but...

    EDIT: Gah... Page 86 in the book describes how you can use precompile.axd to precompile the website. Well, that function was removed after beta 1 as far as I know. Not very impressive...

    The book do have some samples around client-side callbacks which were good, but overall I wouldn't recommend it.

  • [.NET 2.0] How to get Selected-events from an ASP.NET 2.0 TreeView Control using a Web.SiteMap File

    The new ASP.NET 2.0 TreeView control looks neat, and it's very simple to use in conjunction with a web.sitemap file. I spent some time on it yesterday but there were a few things the TreeView control and I never could agree on. For example, when using a TreeView Control in conjunction with a .sitemap file (via the SiteMapDataSource), you cannot get any selection events to fire at all because the treeview is in navigation mode.

    Good news is that it is not impossible to work around it, but it took me a while to figure out, and not without some serious help from Google... The trick is to get the TreeView in selection mode instead of navigation mode. For that to happen, the NavigateUrl property for the node has to be an empty string (""). So, what you have to do is to manually edit the TreeNode DataBindings of the TreeView control and add som code to control the redirection to the right page or else you will stay on the same page forever :)

    From start:

    1. Create your web.sitemap file
    2. Optionally - create your master-page where you want your treeview and breadcrumb controls to sit
    3. Drag/drop a TreeView Control onto the page
    4. Set the ExpandDepth to whatever depth you like 
    5. Choose a new datasource and select a Site Map (the control should now display your nodes from the web.sitemap file)
    6. Select to Edit TreeNode DataBindings
    7. Mark the SiteMapNode from the list of available data bidings and Add it as a selected data binding
    8. Uncheck the Auto-generate data bindings checkbox, we don't need it
    9. Edit the SiteMapNode properties:
      1. Set the TextField to 'Title'
      2. Set the ValueField to 'Url'
      3. Optionally - set any default parameters you like
    10. Finally, add the SelectedNodeChanged event for the TreeView Control and some code in that event to redirect to the right page:

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)

    {

        Response.Redirect(TreeView1.SelectedNode.Value, "false");

    }

    This is a sample of how the TreeView code in the ASPX page could look like:

    <asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" DataSourceID="SiteMapDataSource1"

        PopulateNodesFromClient="False" ImageSet="WindowsHelp" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">

        <DataBindings>

            <asp:TreeNodeBinding DataMember="SiteMapNode" TextField="Title" ValueField="Url" />

        </DataBindings>

        <ParentNodeStyle Font-Bold="False" />

        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />

        <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"

            VerticalPadding="0px" />

        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"

            NodeSpacing="0px" VerticalPadding="1px" />

    </asp:TreeView>

     

    Now if you try out your web site, the SelectedModeChanged event should fire. One big drawback with this piece of code is that now the treeview fires a PostBack event and you have to add the extra Response.Redirect() to get to the right page. Without the redirect the treeview won't be able to automatically detect which page you're on and render the treeview accordingly.

    If you want to add a breadcrumb (SiteMapPath) to the page, just drag/drop it in there. It works out of the box.

  • [.NET 2.0] Membership and Personalization Provider for Access Database

    Not sure why vs2005 didn't ship with providers for Access databases, but Access is quite useful if you want to develop small and simple websites and do not have access to SQL Server or similar. I run a small website hosted by an ISP and even though they support ASP.NET 2.0, I don't have access to SQL Server.

    A sample Access provider is available for download on the MSDN ASP.NET Developer Center section with Provider Toolkit information. I've tried it, and it seems to work good enough for my needs. I'm having some troubles writing to the database under IIS on Windows 2000, even if I set all the security and permission stuff as it should be. Well, as long as it works under Cassini and on my website I don't care :)

    EDIT: Stupid me, even though I had set access rights and permissions on all folders down to APP_DATA, the MDB-files still had the old permissions set on them. I explicitly gave the ASPNET user write access to the file and now it works like a charm :)

  • Virtual Server 2005 R2 Enterprise Edition for Free

    If you by chance missed it - look here for details.Snip from the download page:

    The full software for 32-bit and 64-bit versions is available as a free download. Register to pre-order a CD (available May 2006) or to download the full software.

    Go get it already :)

  • WS-Transfer - Are We Talking HTTP Over XML Over HTTP?

    I may be daft, but after reading the comments on WS-Transfer submission to the W3C, I get the feeling that WS-Transfer feels... I dunno, wrong? I know SOAP is transport independant, but a vast majority of the SOAP calls are made over HTTP, so are we talking HTTP over XML/SOAP over HTTP?

    WS-Transfer is about doing resource manipulation, using HTTP-like verbs such as GET, PUT, DELETE and such. Feels like a more complex REST style of managing resources to me.