Scaffolding in an ASP.NET MVC App#

So at work I had to create a simple admin for a few new features in our app and decided I would try out a few new technologies while I was at it to not only save some time, but to see what is out there now.  My heart was set on this being a simple MVC web app, but really wanted to try ASP.NET Dynamic Data also.  Well good news is that you can use Dynamic Data in MVC fairly cleanly and easily.  By adding a simple route to the global.asax.cs file you can easily have Dynamic Data working although it is web forms based:

 MetaModel model = new MetaModel();
            model.RegisterContext(typeof(MvcApplication1.Models.NorthwindDataContext), 
                new ContextConfiguration() { ScaffoldAllTables = true });
            routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = model
            });

Now this opened my eyes a little that you can easily create a hybrid web app of MVC and Web Forms.  Web Forms does have its advantages sometimes, especially when you start looking at existing tools and controls that you just need to work quickly and don't feel like reinventing the wheel.

You can download a sample project of this working in an mvc app over at codeplex. http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14473

That same night I was playing with Dynamic Data, Rob Conery had just posted his prototype of a Subsonic MVC Add-in for Visual Studio.  I was pretty impressed with the code and views that it generated, and I think is a great start to seeing what is possible.  Both samples took less that an hour to download and implement.  I ended up spending more time integrated security across the multiple web apps on different sub-domains than getting the scaffolds to work.

Wednesday, October 29, 2008 8:10:43 AM UTC #    Comments [0]  |  Trackback

 

jQuery and Microsoft#

I was always hoping this might happen.  Support from Microsoft along with distribution of jQuery with Visual Studio and ASP.NET MVC.  Scott Guthrie and Scott Hanselman along with John Resig have some more detail in their posts.

I plan to update my post, Ajax Control Toolkit and ASP.NET MVC, to use jQuery and the toolkit.  It is about using CSS selectors and the Ajax control Toolkit as an alternative way to use the toolkit in any web development platform.  I modified the Microsoft Ajax library to support CSS selectors, but it is a poor hack and would much prefer to use jQuery anyway.  I was trying to see if I could make it work with the Microsoft Ajax Library from their old preview version from last year.  I was wondering if they would try to reinvent the wheel.  I am glad they decided not to.

Monday, September 29, 2008 4:57:19 AM UTC #    Comments [0]  |  Trackback

 

ASP.NET MVC Firestarter in Tampa#

It was just put out there that David Hayden, Joe Healy and I will be doing a ASP.NET MVC Firestarter here in Tampa, FL on Monday November 17 at the new Microsoft office.  It should be a lot of fun and very informative.  David and I both have live sites in production using various versions of ASP.NET MVC Previews, so we have plenty of real world experience.  So far I have no complaints and it has been very nice that we can modify the source code of the framework if we need to and my team actually has modified one thing that we needed when we went live back in May earlier this year.  Here is some details on what will be covered and how to register:

Throughout the day we will be demonstrating the ASP.NET MVC Framework in a cookbook-style approach with recipes on how to solve common challenges when developing MVC web applications. No previous knowledge or experience is necessary. We will walk you through the basics on creating views and controllers and by the end of the day show you how to develop end-to-end MVC applications complete with Ajax, authentication, authorization, caching, databinding, logging, persistence, validation, and other common challenges we experience in day-to-day development.

Sample code will leverage and integrate popular frameworks and libraries like ADO.NET Data Services, ASP.NET AJAX, ASP.NET Dynamic Data, Enterprise Library, Entity Framework, and LINQ To SQL to show you how to write less code and be more productive during your development.

Polish it all off with examples showing the extensibility of the MVC Framework using custom controller factories, alternative view engines, and custom action filter attributes just to name a few.

Topics covered will include “How Do I...”

- Create Views Easily? ( HTML and Url Helpers )

- Handle Get and Post Requests? ( simple databinding of action method arguments, ActionResults, etc.. )

- Pass Data Between Views and Controllers? ( ViewData and TempData )

- Bind Views and Forms to complex data types? ( ModelBinders )

- Handle Errors Gracefully? ( ActionFilter Attributes )

- Provide Input Error Validation? ( ValidationMessage, ValidationSummary, ViewData.ModelState )

- Handle Authentication and Authorization? ( ActionFilter Attributes and Membership Provider )

- Persist to a database ( LINQ To SQL, Entity Framework )

- Log Messages to Database, File, EventLog ( ActionFilter Attributes, etc. )

- Leverage AJAX and JSON? ( ASP.NET AJAX and jQuery )

- Leverage DynamicData? ( Futures )

Some of the more complex and non-beginner topics can be discussed if time is allowed and/or maybe discussed afterwards in a social environs…

- Alternate View Engines

- IoC and Custom Controller Factories

- Unit Testing

Bring your USB Flash Drive to grab the sample code and begin developing ASP.NETMVC Web Applications today!

 

Microsoft Office – Tampa, FL
5426 Bay Center Drive

Suite 700

Tampa, FL 33609

Monday, November 17, 2008

9:00 AM–4:30 PM

Click here to register or call 877.673.8368 with event ID1032390730.

Friday, September 26, 2008 5:54:28 PM UTC #    Comments [2]  |  Trackback

 

Testing code by precompiling view pages in asp.net mvc#

I am loving to use ASP.NET MVC at work and have several projects that we have live right now.  We are also migrating our web forms app very slowly by adding more mvc like views in our web forms so they can be ported easier down the road.  The one issue I am having so far is testing the code that is in the pages for the mvc apps and the code in the web form views.  Runtime errors are so annoying and when on a decent size team, it is almost impossible to test every page at runtime, especially when you don't always know what another developer added or changed. 

So I wrote a test that precompiles the entire app and all the code in the aspx page.  It is more of a sanity check than anything and has been very helpful.  It has caught several simple errors that would have ended up in production and am very thankful for having this sanity test.  Here is the sample test code that you can add to your test project:

        const string ASPNET_COMPILER_PATH = "C:/Windows/Microsoft.NET/Framework/v2.0.50727/aspnet_compiler -p {0} -v {1}";
        const string TERMINAL_APP_NAME = "cmd.exe";

        [TestMethod]
        public void Precompile_all_pages()
        {
            string sitePath = @"D:\websites\mywebsite.com\preview";
            string siteNode = "preview.mywebsite.com";

            string ret = compileProject(sitePath, siteNode);

            string msg = "Utility to precompile an ASP.NET application\r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n";
            Assert.AreEqual(msg, ret);


        }

        public string compileProject(string compileDir, string iisApplicationPath)
        {
            string compileString = string.Format(ASPNET_COMPILER_PATH, compileDir, iisApplicationPath);

            Process proc = new Process();
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.FileName = TERMINAL_APP_NAME; //File name to execute
            proc.StartInfo.Arguments = "/c " + compileString;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.Start(); // Execute process
            proc.WaitForExit();

            string output = proc.StandardOutput.ReadToEnd();

            return output;
        }
Make sure to reference System.Diagnostics.  You can also add the aspnet_compiler to your post build in your cproj file, although this will slow down your building of your app locally, but is a good practice I think for working with mvc.
Saturday, August 30, 2008 8:12:47 AM UTC #    Comments [0]  |  Trackback

 

Ajax Control Toolkit with ASP.NET MVC#

I have been playing with ASP.NET MVC for about 4 months now and have a couple sites, one decent sized, in production right now.  I am very comfortable with it and the extra control over the html is very nice.  Since there is no postback and viewstate in MVC, the Ajax Control Toolkit does not work as designed and needs a few changes to make it work within the MVC framework.  It has forced me to look at jQuery more, and I am really enjoying the simplicity of jQuery's design and the power of using it.  I was just wishing I could use some of the controls in the toolkit and of course i wanted to see if i could get it to work for the sake of making it work.  Well I did get it to work for 2 of the controls so far, some that i use in a normal project.  They are the Calendar and TextBoxWatermark. 

To make this easier, and from a lesson I learned with jQuery, it is best to select your DOM elements by a css class when you can.  This allows for your JavaScript to be more unobtrusive, and ultimately cleaner and easier to maintain.  So i took a couple of methods from the Ajax Futures from last July 2007 and put it in a helper class along with a simple function of mine to allow selection of DOM elements by css very trivial.

To give you an idea of what i am talking about using css selectors for the toolkit, look at this sample:

var elements = $select('TextboxWatermark');
    for (i=0;i<elements.length;i++)
    {
        var elem = elements[i];
        $create(AjaxControlToolkit.TextBoxWatermarkBehavior, {"WatermarkText":elem.title}, null, null, $get(elem.id));
    }

 

I created a helper function $select(className) which basically wraps Microsoft Ajax Futures getElementsByClassName like such:

function $select(className)
{
    var elements = Sys.UI.DomElement.getElementsByClassName(className);
    return elements;
}

So basically if you put the class name TextboxWatermark on any textbox input with the title you like to show as the watermark like this:

<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />

or

<%= Html.TextBox("username", new { @class = "TextboxWatermark", @title = "Must be at least 6 chars" }) %>

What is nice about the second option is that you get the added benefit of getting the View Engine to fill out the value of the textbox if there is an item in ViewData of the ViewData.Model that has a var named 'username'.

So I find this much easier than having to add the control and on each page and for each DOM element i want to attach this behavior to. 

When you want to do the same thing with other controls, you need to know how to call it in JavaScript.  This is the part that is kind of hidden when you are using controls like in WebForms.  If you go to the AjaxControlToolkit Sample Site, and view page source on one of the controls example pages, you will see what JavaScript is generated.  Go to the TextboxWatermark example and then view source from the browser.  You will see 5 of these at the bottom for each control

Sys.Application.initialize(); Sys.Application.add_init(function() { $create(AjaxControlToolkit.TextBoxWatermarkBehavior, {"ClientStateFieldID":"ctl00_SampleContent_TextBoxWatermarkExtender1_ClientState","WatermarkCssClass":"watermarked","WatermarkText":"Type First Name Here","id":"ctl00_SampleContent_TextBoxWatermarkExtender1"}, null, null, $get("ctl00_SampleContent_TextBox1")); });

So this is how you can see how to invoke that control with the arguments, and then of course looking at the source files help you see what else they are doing.  I am already feeling cleaner with the JavaScript since i have just one function that now does it for every Dom element with the class name, "TextboxWatermark".  Here is the function that gets called on pageLoad.  This will attach the behavior for each Dom element.

function AddAllTextboxWatermarks()
{
    var elements = $select('TextboxWatermark');
    for (i=0;i<elements.length;i++)
    {
        AddTextBoxWatermark(elements[i]);
    }
}

function AddTextBoxWatermark(elem)
{
    $create(AjaxControlToolkit.TextBoxWatermarkBehavior, {"WatermarkText":elem.title}, null, null, $get(elem.id));
}

Now the Calendar was just as easy to put in the form.  Here is how I implemented the Calendar:

<%= Html.TextBox("calendar", new { @class = "Calendar" })%>

Now, the calendar was a little tougher to get to work cause of getting the JavaScript just right cause of the amount of files needed.  The Calendar is one of the more complex controls since it uses a popup behavior, animation, timer and threading among others. 

So, that solves simple controls, but what about other controls that need overloads and are not as simple parameter wise.  Well I put all these controls in html helpers also that inject the JavaScript at run time.  I actually started this way, but after using jQuery a little, i really wanted to see if i could implement this using css selectors with the asp.net Ajax framework.  Also it felt like black box controls were creeping back in, but sometimes we need to generate html in c#, so i think it is just about finding the fine line when not to do it and when it is simple and reusable.

Here this an example of the TextboxWatermark as a Html helper:

<%= Html.TextboxWatermark("Enter Username", "username", true) %>

So basically this returns you the input text box,  with the TextboxWatermark behavior attached.  Also it will make sure that the appropriate .js files are in the page along with the html needed to make it work with this particular control.  Now this creates a lot more code in the page just like the controls now do in the existing toolkit, but has its own advantages.  We can create overloads and add additional parameters without having to write JavaScript.  Some people actually don't like to write JavaScript, it is either intimidation or fear of messing up something I guess.  Personally I have used it since the late 90's and is the only language that I have used for 10 years.  Well that and SQL. 

The Calendar control has advantages to being used as a Html helper since we can have several overloads to define format among other parameters that a calendar control can take.  Here is how you set the format:

<%= Html.Calendar("bDay", "MM/dd/yyyy") %>

Also lets say you want a button to invoke the menu, not just clicking in the textbox.

 <%= Html.Calendar("calBtn", "txtDate", "MM/dd/yyyy")%>
 <input type="text" id="txtDate" name="txtDate" />
 <input type="button" id="calBtn" name="calBtn" value="Pick Date" />

Here is a download of a working project with all the necessary files you need to make these controls work either with CSS Selectors or with Html helpers.  I basically created a new mvc web app from scratch using their new templates in Preview 4 that has the membership stuff in it, so that it works with the base starter project that Microsoft provides. 

Download: http://www.jimzimmerman.com/downloads/MvcPreview4AjaxToolkit.zip

Tell me what you think.  I hope you find it useful.  I am leaning towards doing everything with CSS Selectors, but am finding it pretty helpful to use Html helpers also.  I am working on Collapsible Panel and Popup Control next.  I know the Microsoft Ajax team is working on some things with the Toolkit also and look forward to seeing how they integrate it.

Saturday, August 16, 2008 6:27:40 AM UTC #    Comments [1]  |  Trackback

 

Subsonic 2.1 Final is Released#

Yeah!  Subsonic 2.1 Final is released, now time to start comparing it with my version of 2.1 that we have been using in production for months.  The features are so cool and would not even think of switching to Linq if I had to.  Download it here: http://www.codeplex.com/subsonic/Release/ProjectReleases.aspx?ReleaseId=14067

The migrations look very cool and I cant wait to finally start using migrations instead of manually updating SQL by schema comparisons.  Check out a demo here:http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/

Wednesday, July 09, 2008 6:35:42 AM UTC #    Comments [0]  |  Trackback

 

Very Cool Free .NET E-Book#

I just saw a twitter from hanselman on this very cool free e-book on alt.net practices.  It is about 80 pages and very professional.  I would easily pay for this book and am going to have all of my devs in the company read it.

 The Foundation Of Programming Series Free e-book By Karl Seguin

I have just ready the first few chapters so far and would like to read the whole thing in one sitting.  Too bad wife is not game for me to read on my laptop the whole night tonight. :)

I am wondering when I get a kindle if that will be different since I can't "work" on the kindle.

Monday, July 07, 2008 5:33:51 AM UTC #    Comments [0]  |  Trackback

 

TempData not working with SQL Server Session State and ASP.NET MVC Preview 3#

We are extensively using ASP.NET MVC Preview 3 for one of our web projects and we are loving it.  The other day when we deployed our app to our web farm, we realized that we got an error when typing in a wrong password and that our app was not returning the handled error of improper login and the app was blowing up with the error, "Unable to serialize the session state."  When we tracked it down we realized that this only happened when using TempData instead of ViewData. 

So, when redirecting to an action in MVC, you will lose anything you have in ViewData so if you want to carry an error to another action and show it in the next view that is rendered, then you will need to create your own session variables or use the built in TempData which works great except when you are using SQL server for session state instead of in-proc.  Well we have no choice than to use SQL server for session state since we have several web servers on our web farm. 

So, since ASP.NET MVC source code is available on Codeplex, I decided to investigate what class makes up TempData.  I found that TempData uses an internal class called Pair and it is not marked as serializable.  Dare I try to fix it and run with our own version of System.Web.Mvc?  Why not, it is just C#?  the fix was very easy and after adding the serializable attribute to the Pair class.  Pair.cs found in the folder Util inside of the MVC project.

namespace System.Web.Util {
    using System;

    // Generic Pair class.  Overrides Equals() and GetHashCode(), so it can be used as a dictionary key.
    [Serializable] 
    internal sealed class Pair<TFirst, TSecond> {

 

All the tests now passed and most importantly, our site works perfectly.  It was very empowering to add a simple change to the framework and not have to wait for a fix from Microsoft.  I feel so much more comfortable using frameworks that have the source code with them.  That is why we use ASP.NET MVC and Subsonic 2.1.

Friday, June 27, 2008 7:21:40 AM UTC #    Comments [0]  |  Trackback

 

 

All content © 2009, Jim Zimmerman
Book
New Book
Links to me
On this page
Sponsors
Calendar
<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Archives
Tags
Blogroll OPML
Technorati
Favorite Links
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail