15 Jun

Learning Asp.Net MVC Framework

Category:UncategorizedTag: , :

This is basically a ?What is Chris up to now?? post.  As the title suggests I?m going up to my ears into Asp.Net MVC.  I?ve been doing Asp.Net WebForms for about the past 8 years, so this is a good time for a switch.  Lucky for me, I?m armed with a good book: Pro Asp.Net MVC Framework by Steven Sanderson.  I?ve read about have of it and I love it.   The greatest thing about the book is that it actually answers questions as you think of them.  A remarkable achievement for any technical book.

Pro ASP.NET MVC Framework: Steven Sanderson: Books

ISBN: 1430210079
ISBN-13: 9781430210078

But here is where I?m at right now (btw: if I get anything here completely wrong, blame me, not the book author):

1. Routing.  I?ve already played with routing before with standard Asp.Net WebForms, so the concept is not new to me.  But getting to the point where I really grok Routing will be a while.  The basic concept is this: Routing is one of the first control points that you get with Asp.Net MVC (or WebForms now as well).  When a request comes in (web browser requests a page) the Routing engine determines which controller (or web form) will handle the request.  With this in place you can now have ?pretty? urls.  So something like https://elegantcode.com/Chris/is/cool.  That link wont work, but that is the general idea.  More often than not you see something like this: http://localhost/Customer/Edit/ChrisBrandsma

* my current issues: none really ? so long as I can manage to follow the existing route paradigm.  Passing extra parameters can be strange, but the good old ?id=5&sid=6&? still works.  Also, as soon as you head into Routing land you suddenly have to start thinking about your urls.  In traditional WebForms world, a url is like a latitude/longitude ? and the user has to figure out how to get there.  In Routing land, the url is a list of turn by turn directions.

2. Controllers.  Controller are the general housing points for web application flow.  If you look at the second url, you see /Customer/Edit/ChrisBrandsma there.  The Customer part of the url is the controller.  Why?  Because my Routing setup says it is.  That?s why.  Controllers hold Actions.  Edit is the action.  ChrisBrandsma is just some data I?m passing to the action.

* My Current Issues: I have not reached a Zen of when to create a new controller yet.  I keep thinking of single responsibility, so right now my controllers tend to have few methods in them.  But I?m still getting the hang of it.

3. Actions.  Actions are things the Controller can do.  Back to the url example: http://localhost/Customer/Edit/ChrisBandsma, there is an expectation I will be editing myself (I would like 50 more IQ points and Kung Foo).  Edit is the action here.  There is also a convention that states there should be an Edit.aspx page to go along with the Edit action.  I can override that, but that is the expectation again.

There are various things an action can return (and they have to return something): JavaScript, JSON, text, Files, html, etc.  But it seems their primary function is to get a view and hand it data.

* My Current Issues: I?m barely scratching the surface with this one.  I am trying very hard to keep my Action code small.  To that end I?m creating ControllerActionPresenters and ControllerActionViewModel for each Action I create.  This might be overkill, but it is keeping thinks clean right now. 

So now my Actions have three tasks: get user data (querystring, form, session, etc), get the presenter and pass it the user data, return the ViewModel data to a specific view.  I?m ok with this so far.

4. Views.  I have no zen here.  I remember the bad old asp days, and I am eager not to relive them.  I?m keeping my JavaScript and css in separate files, but tag soup is upon me.  Everywhere I look there is <% code here %> and less innocuous <%=View.DataModel.SomeValue %>.   I need to read up on how to separate thing out into specific controls or I am going to go nuts.  HtmlHelpers are my friends, HtmlHelpers are my friends.

Also,  I?m doing everything I can to use typed Views.  This would be insane without typed views.

* My (other) current Issues: so far I have not tried to do a data entry form with MVC.  Until I do that, more of this will be a mystery to me.

My thoughts so far.  I still like it. There are some learning curves ahead for me yet, but I enjoy that.  Either I will savor the sweetness of WebForms from the experience, or look back at them and laugh.  I?m starting to laugh.

9 thoughts on “Learning Asp.Net MVC Framework

  1. I’m finding things very similar to you, Controllers I’m ok wiht, there’s a lot of guidance out there on the responsibilties and best way to work with Controllers.

    Views on the other hand I’m finding there’s very little guidance, especially on things beyond the simple scenarios. There’s so many ways to approach this problem I can very easily see myself shooting myself in the foot. Right now I’m working on a simple contact form, but adding in client side validation with ajax submission and dealing with a json result can certainly tie things up.

  2. Hi Chris,

    I feel a common misconception is to inmediately associate tags with spaghetti code. What you’re doing in your view is merely using a template to merge dynamic content. The View still only has one responsibility, which is to display information and as long as the code you have in your views is for that purpose, I see nothing wrong with it.

    Whether you use Spark or any other kind of engine, in the end you’re doing the same thing, just with a different syntax.

    Also, I highly recommend you use ViewModels and not directly access your entities in your views. I blogged about this over on my site if you want to know more.

  3. Nigel I agree with you,
    adding client side validation to a form with ajax submission and also dealing with a json result is a very frustrating task.
    For combining all the things together you can use online IDE like coderun.com and also debug online client and server side code.

Comments are closed.