8

what are your top lessons learned when starting asp.net mvc that you would highlight to someone starting out so they can avoid these mistakes?

2
  • 2
    One lesson that anybody using SO should learn is some questions are Wiki! Commented Sep 7, 2009 at 17:56
  • Recommended reading: get this presentation about Patterns and Anti-Patterns in ASP.net MVC: indomitablehef.com/?p=225 Commented Sep 7, 2009 at 18:18

11 Answers 11

8
  • Use Html.Encode() everywhere you print data, unless you have a very good reason to not do so, so you don't have to worry about XSS
  • Don't hardcode routes into your views or javascripts - they're going to change at some point, use Url.Action() instead
  • Don't be afraid of using partial views
  • MVC is no silver bullet, first evaluate if it's indeed the best tool of choice for solving your problem.
Sign up to request clarification or add additional context in comments.

Comments

4

Don't forget the "Unit Tests" part of the pattern.

Comments

3

Try to always use a ViewModel to pass data between the Controller and the View. You may think you don't need one, you can just pass your model around, but suddenly you need a list box with several options for editing a model, or displaying a message (not validation message) and you start adding items to the ViewData, with magic strings as keys, making the app harder to maintain. There are also some security issues that you solve with a ViewModel. For instance:

class user:
int id
string name
string email
string username
string password

Your view let's the user change his name and email and posts to the action

public ActionResult Edit(User user)
{
--persist data
}

Someone could tamper your form and post a new password and username and you will need to be very careful with the DefaultBinder behavior. Now, if you use a ViewModel like:

class userEditViewModel:
int id
string name
string email

The problem is gone.

2 Comments

Why not excluding the 'username' and 'password' on action's filter? Or update model with just the list of fields you want to update?
That's perfectly valid but you can forget to do so, some new dev may not know about this since is not that obvious. Using ViewModels it's almost impossible to fail.
1
  • Whenever it is possible make your view typed

  • Avoid logic in your views

  • stay away from the HttpContext

Comments

1
  1. Get Steve Sandersons Pro ASP.NET MVC Framework

  2. Debug into the Sourcecode

Comments

1
  • If you make a Controller method with a different parameter name from id for a single parameter method, you have to make a new route. Just bite the bullet and use id (it doesn't care about the type) and explain it in the comments.
  • Makes sure you name your parameters with RedirectToAction :

    return RedirectToAction("DonateToCharity", new { id = 1000 });

  • You lose your ViewData when you RedirectToAction.

Comments

1

Put javascript in seperate files, not into the view page

1 Comment

Why? What if I need to assign js variables in view page? i.e. id of current item, or some generated url? I agree that MOST of javascript (functions) should be in *.js files, but lot of variables, localized messages etc you can generate (if possible all in one partial view which is section of layout) in cshtml/ascx
0

name of the controller :)

unit test Pattern

Comments

0

Don't use the Forms collection, use model binding.

Try not to use ViewData, create a ViewModel.

If you have a loop or an if in your View, write an HTML helper.

Kindness,

Dan

Comments

0

Don't let your controller become a fat one and do too much work. I've seen 1000+ line controllers in the past and it just becomes an absolute nightmare to understand what's going.

Utilise unit testing for your controllers to ensure that dependencies are kept under control and that your code is testable.

Don't get drawn into letting jQuery and fancy clientscript define the behaviour of your application, try and use it as sparingly as you can and let it enhance your application instead.

Use partial views and HTML helpers whenever possible to ensure that your Views do not become unwieldy and a maintenance nightmare.

Use a ViewModel whenever possible.

Use a dependency injection framework to handle your dependencies (MvcContrib has several controller factories, though it's simple enough to roll your own).

Comments

0

Use a different controller for every section of your site (e.g., Home, Account)

Learn how to use ViewData and TempData

Learn what's the use of RenderPartial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.