This month I’ve been going pretty hard core about learning ASP.NET and I was having a lot of difficulty with playing with the ASPX view engine. I’m already fairly familiar with HTML and CSS, but now I have to learn these new ASP.NET controls? Why can’t I just stick to the HTML and CSS I know and enjoy working with?
Well it turns out I can… I don’t need to learn about the complex data binding of a GridView or how an ASP:TextBox converts to a plain ol’ input tag wrapped in span tags. I’ve been heavily studying the work from the project we worked on during the Nothin’ But .NET boot camp and reading up on the MSDN documentation on inlining aspx.
I like it… C’mon doesn’t this….
Seem a lot more readable then this…
The extra asp namespaces appended to all the controls just seem like noise. The explicit casting from Container.DataItem to the type actually contained seems like extra work. I suppose you could write all this out in an item data bound event handler. But it just looks like noise, noise noise… Then when you actually look at the HTML that’s spit out it loaded with more noise…
Well all of this can be avoided. But you’re going to have to get just a little bit deeper, and here’s how. Everything you need to know is in HttpContext. Yup!
For my current school assignment I’m currently flushing out the concept of an HttpGateway, where all traffic comes in and all traffic goes out. Currently this is just wrapping and HttpContext but with an interface that works better for me as a client of it. I don’t need to see everything available in the HttpContext (which is a lot!).
Next up, take advantage of the HttpContext.Items collection. This is a collection of objects which you can load up with extra information to pass on down the pipeline. Consider the concept of view luggage, where luggage is issued a ticket when you drop it off. You can then claim that luggage using the same luggage ticket.
What does this mean, you can have your presenters, controllers, commands (whatever) drop off luggage to be carried to the view. You can then have your views claim that luggage with the luggage ticket. (Take a look back at the first bit of HTML markup in this post.)
The HttpContext.Items collection is a dictionary for objects. Using strongly typed luggage tickets you can make that the key, and the actual luggage the value to store in the items collection. The HttpContext then becomes the airplane, carrier or transporter that hauls your luggage from your presenter, controller, command (whatever) to the view.
For the quick and dirty here’s the code that describes what I’m talking about:
To parse out the value submitted in a request you can create mappers to parse out the values for each control. In the code below each of the string literals prefixed with a “ux” represents the name of the html control that was on the page.
Next up, figuring out a cleaner way to implement authorization and authentication without having to rely on the FormsAuthentication class. So far my spiking of Forms Auth looks something like the code below… *Please do not use the code below. I was just trying to understand how forms auth works and this is not a great example of how you should use it.