Introducing ASP.NET MVC 3 (Preview 1)

This morning we posted the “Preview 1” release of ASP.NET MVC 3.  You can download it here.

We’ve used an iterative development approach from the very beginning of the ASP.NET MVC project, and deliver regular preview drops throughout the development cycle.  Our goal with early preview releases like the one today is to get feedback – both on what you like/dislike, and what you find missing/incomplete.  This feedback is super valuable – and ultimately makes the final product much, much better.

ASP.NET MVC 3

As you probably already surmised, ASP.NET MVC 3 is the next major release of ASP.NET MVC. 

ASP.NET MVC 3 is compatible with ASP.NET MVC 2 – which means it will be easy to update projects you are writing with MVC 2 to MVC 3 when it finally releases.  The new features in MVC 3 build on top of the foundational work we’ve already done with the MVC 1 and MVC 2 releases – which means that the skills, knowledge, libraries, and books you’ve acquired are all directly applicable with the MVC 3 release.  MVC 3 adds new features and capabilities – it doesn’t obsolete existing ones.

ASP.NET MVC 3 can be installed side-by-side with ASP.NET MVC 2, and you can install today’s “Preview 1” release on your machine without it impacting existing MVC 2 projects you are working on (they will continue to use MVC 2 unless you explicitly modify the projects to retarget them to MVC 3).  When you install “Preview 1” you will have a new set of ASP.NET MVC 3 project templates show up within Visual Studio 2010’s “New Project” dialog – choosing one of those when you create a new project will cause it to use MVC 3.

Below are details about some of the new features and capabilities in today’s “Preview 1” release.  Unless otherwise noted, all of the features I describe are enabled with the preview build you can download and use today.  More ASP.NET MVC 3 features will come in future preview refreshes as we flesh out the product more and iterate on your feedback.

View Improvements

ASP.NET MVC 3 “Preview 1” includes a bunch of view-specific improvements.

Add->View Dialog

“Preview 1” includes a new “Add->View” dialog that makes it easy for you to choose the syntax you want to use when you create new view template files.  It allows you to select any of of the available view engines you have installed on your machine – giving you the ability to use whichever view templating approach feels most natural to you:

AddView9

There are a bunch of great open source view template engines out there (including Spark, NHaml, NDjango and more) – it is now much easier for them to integrate into Visual Studio.

Today’s “Preview 1” build of ASP.NET MVC 3 comes with two view-engine already pre-enabled within the dialog: ASPX and Razor. 

New “Razor” View Engine

Earlier this month I blogged about the new “Razor” view engine we’ve been working on.  Based on the comments in the post, a lot of people are eagerly waiting to use it.  The good news is that you can start using it with today’s “Preview 1” release.

Simple Razor Example

Let’s build a super-simple store site that lists product categories, and allows visitors to click the categories to see a listing of products within them.  You can download a completed version of this sample here.

image

Below is a StoreController class that implements the two action methods (“Index” and “Browse”) needed to build the above scenario:

image

We’ll use the new “Razor” view engine to implement the view templates for our StoreController.

Below is the “Layout.cshtml” layout-page that will define the common layout UI we want across our site.  The “RenderBody()” method indicates where view templates that are based on this master layout file should “fill in” the body content:

image

Below is the view template for the Index action.  It is based on the above layout page, and outputs a <ul> list of category names: 

image

The template above is using the standard Html.ActionLink() helper method in ASP.NET MVC to render a hyperlink that links to the “Browse” action method of our StoreController.  All of existing HTML helper methods in ASP.NET MVC work in “Razor” views – this is true both for the HTML helper methods built-into ASP.NET MVC, as well as those built by others (including vendors and the MvcContrib project).

Below is the view template for the Browse action.  It lists the products within a specific category:

image

Notice above how we are using the “Model” property within our foreach statement to access the strongly-typed List of products we passed from our Controller.  We are doing this just like we would within .aspx view templates.  Razor also supports a “View” property which allows us to access un-typed “ViewData” passed to the view template.  “View” is a dynamic property (a new feature of .NET 4) – which gives us a slightly cleaner syntax when accessing ViewData.  Instead of writing ViewData[“Cateogry”] we can now just write View.Category.

Clean and Concise

The code in the screen-shots above contains everything we need to write to implement our Controller + Views.  “Razor” helps make view templates clean and concise, and I think you’ll find it enables a very fluid coding workflow. Read my “Razor” blog post from earlier in the month to learn more about the syntax and understand how it works.  You can download a running version of the above sample here.

Code Intellisense and Colorization

One of the things you might have noticed from the screen-shots above is that “Razor” file colorization and code intellisense is not yet supported in Visual Studio with today’s “Preview 1” release.  We will be enabling full code intellisense and colorization with a future preview refresh.  The VS 2010 editor will support Razor file intellisense for C#/VB code, as well as for HTML/CSS/JavaScript. 

Other Improvements in the Future

Three other enhancements we are working to enable in a future preview refresh are:

  • The ability to use a @model statement at the top of a “Razor” file instead of having to explicitly inherit from a base class.  This reduces the code and simplifies it.  
  • The ability to specify a default LayoutPage for the site to avoid having to explicitly set it within each view template.  This further reduces the code within the view template, and makes your code more DRY.
  • The ability to unit-test individual “Razor” template files without having to run the application or launch a web-server.

With these first two changes the above Browse template will be able to be written as simply:

image

The above template syntax will be supported in a future preview refresh.  Full colorization and code-intellisense will be provided within the editor.

Controller Improvements

ASP.NET MVC 3 “Preview 1” includes several nice controller-specific enhancements.

Global Filters

ASP.NET MVC supports the ability to declaratively apply “cross-cutting” logic using a mechanism called “filters”.  You can specify filters on Controllers and Action Methods today using an attribute syntax like so:

image

Developers often want to apply some filter logic across all controllers within an application.  ASP.NET MVC 3 now enables you to specify that a filter should apply globally to all Controllers within an application.  You can now do this by adding it to the GlobalFilters collection.  A RegisterGlobalFilters() method is now included in the default Global.asax class template to provide a convenient place to do this (it is then called by the Application_Start() method):

image

The filter resolution logic in MVC 3 is flexible so that you can configure a global filter that only applies conditionally if certain conditions are met (for example: debugging is enabled, or if a request uses a particular http verb, etc).  Filters can also now be resolved from a Dependency Injection (DI) container – more on that below.

New Dynamic ViewModel Property

ASP.NET MVC Controllers have supported a “ViewData” property that enables you to pass data to a view template using a late-bound dictionary API.  For example:

image

The “ViewData” API is still supported in ASP.NET MVC 3.  MVC 3 augments it, though, with a new “ViewModel” property on Controller that is of type “dynamic” – and which enables you to use the new dynamic language support within VB and C# to pass ViewData items using a slightly cleaner syntax than the current dictionary API.  Now you can alternatively write the following code to achieve the same result as above:

image

You do not need to define any strongly-typed classes to use the ViewModel property.  Because it is a “dynamic” property you can instead just get/set properties on it and it will resolve them dynamically at runtime.  It internally stores the property name/value pairs within the ViewData dictionary.

New ActionResult Types

ASP.NET MVC 3 “Preview 1” includes several new ActionResult types and corresponding helper methods.

HttpNotFoundResult

The new HttpNotFoundResult class is used to indicate that a resource requested by the current URL was not found. It returns a 404 HTTP status code to the calling client. You can optionally use the new HttpNotFound() helper method on Controller to return an instance of this action result type, as shown in the following example:

image

Permanent Redirects

The HttpRedirectResult class has a new Boolean “Permanent” property that is used to indicate whether a permanent redirect should occur. A permanent redirect uses the HTTP 301 status code.  In conjunction with this change, the Controller class now has three new methods for performing permanent redirects: RedirectPermanent(), RedirectToRoutePermanent(), and RedirectToActionPermanent().  These methods return an instance of HttpRedirectResult with the Permanent property set to true.

HttpStatusCodeResult

The new HttpStatusCodeResult class can be used to set an explicit response status code and description. 

JavaScript and AJAX Improvements

ASP.NET MVC 3 includes built-in JSON binding support that enables action methods to receive JSON-encoded data and model-bind it to action method parameters. 

To see this feature in action, consider the jQuery client-side JavaScript below.  It defines a “save” event handler that will be invoked when a save button is clicked on the client.  The code within the event handler constructs a client-side JavaScript “product” object with three fields whose values are retrieved from HTML input elements.  It then uses jQuery’s .ajax() method to POST a JSON based request containing the product to a /Store/UpdateProduct URL on the server:

image

ASP.NET MVC 3 now enables you to implement the /Store/UpdateProduct URL on the server using an action method like below:

image

The UpdateProduct() action method above accepts a strongly-typed Product object as a parameter.  ASP.NET MVC 3 can now automatically bind the incoming JSON post values to the .NET Product type on the server – without you having to write any custom binding or marshalling logic.  ASP.NET MVC’s built-in model and input validation features all work as you’d expect with this.

We think this capability will be particularly useful going forward with scenarios involving client templates and data binding (like I’ve previously blogged about here).  Client templates will enable you to format and display a single data item or set of data items by using templates that execute on the client.  ASP.NET MVC 3 will enable you to easily connect client templates with action methods on the server that return and receive JSON data.

Other JavaScript/AJAX Improvements in the Future

Future preview refreshes of ASP.NET MVC 3 will include better support for unobtrusive JavaScript.  ASP.NET MVC 3 will also directly support the jQuery Validation library from within its built-in validation helper methods.

Model Validation Improvements

ASP.NET MVC 2 came with significant model validation improvements.  You can read my previous blog post to learn more about them.

ASP.NET MVC 3 extends this work further, and adds support for several of the new validation features introduced within the System.ComponentModel.DataAnnotations namespace in .NET 4. In particular:

  • MVC 3 supports the new .NET 4 DataAnnotations metadata attributes such as DisplayAttribute.
  • MVC 3 supports the improvements made to the ValidationAttribute class in .NET 4.  The ValidationAttribute class was improved in .NET 4 to support a new IsValid overload that provides more information about the current validation context, such as what object is being validated.  This enables richer scenarios where you can validate the current value based on another property of the model. 
  • MVC 3 supports the new IValidatableObject interface introduced in .NET 4.  The IValidatableObject interface enables you to perform model-level validation, and enables you to provide validation error messages specific to the state of the overall model, or between two properties within the model. 

Below is an example of using the IValidatableObject interface built-into .NET 4 to implement a custom validation method on a class.  This method can apply validation rules across multiple properties and yield back multiple validation errors (and optionally include both an error message like below as well as a list of property names that caused the violation):

image

ASP.NET MVC 3 now honors the IValidateObject interface when model binding (in addition to all of the other validation approaches it already supported with MVC 2), and will retrieve validation errors from it and automatically flag/highlight impacted fields within a view using the built-in HTML form helpers:

image

ASP.NET MVC 3 also introduces a new IClientValidatable interface that allows ASP.NET MVC to discover at runtime whether a validator has support for client validation.  This interface has been designed so that it can be integrated with a variety of validation frameworks.  MVC 3 also introduces a new IMetadataAware interface that simplifies how you can contribute to the ModelMetadata creation process. 

Dependency Injection Improvements

ASP.NET MVC 3 provides better support for applying Dependency Injection (DI) and integrating with Dependency Injection/IOC containers.

In “Preview 1”, we’ve added support for dependency injection in the following places:

  • Controllers (registering & injecting controller factories, injecting controllers)
  • Views (registering & injecting view engines, injecting dependencies into view pages)
  • Action Filters (locating & injecting filters)

For future previews we are investigating adding dependency injection support for:

  • Model Binders (registering & injecting)
  • Value Providers (registering & injecting)
  • Validation Providers (registering & injecting)
  • Model metadata Providers (registering & injecting)

ASP.NET MVC 3 will support the Common Service Locator library, and any DI container that supports it’s IServiceLocator interface.  This will make it really easy to integrate any DI container that supports the Common Service Locator with ASP.NET MVC.

Note: In Preview 1, we redefined the CSL interface in our codebase, and didn’t include the CSL DLL in our setup. This means that existing implementations of CSL won’t “just work” with “preview 1” – instead they’ll have to recompile their CSL implementations against our interface to make them work. Future preview refreshes will make this CSL library dependency easier, and avoid this extra step.

Brad Wilson is starting a great blog series on ASP.NET MVC 3’s Dependency Injection Support.  Below are links to his first few articles about it:

Click here to download a simple ASP.NET MVC 3 example that demonstrates how to use the popular Ninject Dependency Injection Container with ASP.NET MVC 3. 

Downloads and Links

Click here to download ASP.NET MVC 3 Preview 1.  Post feedback/issues about it in the ASP.NET MVC Forum.

Once ASP.NET MVC 3 is installed, you can download and run the simple Razor sample I demonstrated in the blog post above. 

Read my previous “Razor” blog post to learn more about how it works and its syntax.  Also read my recent EF4 Code-First and EF4 Code-First Schema Mapping posts to learn more about the database code and clean model layer I built using EF4 Code-First and SQL Express within the above sample. Listen to Scott Hanselman’s Podcast about ASP.NET MVC 3 with Phil Haack, and watch this Scott Hanselman Channel 9 video about ASP.NET MVC 3.

Summary

We are excited to get today’s ASP.NET MVC 3 “Preview 1” release in people’s hands, and start receiving feedback on it. 

Our primary goal with these early preview releases is to get feedback – both on what you like/dislike, and what you find missing/incomplete.  This feedback is super valuable – and ultimately makes the final product much, much better.  If you do install today’s “Preview 1” build, please post your feedback and any bugs/issues you find to the ASP.NET MVC forum at http://forums.asp.net.  The team will be monitoring this forum closely, and will be happy to help with anything you run into. 

We will then iterate on the feedback you send us, and further refine ASP.NET MVC 3 in future preview refreshes.

Hope this helps,

Scott

P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu

123 Comments

  • Great...this will go miles ahead.

  • Awesome!, great work.

    Mike

  • Hi Scott! Kudos to all the team! Great work, it seems delicious! Is the roadmap already known?

  • Ok. It's time to learn about ASP.NET MVC. Very good work.

  • The changes all look great, Scott. But do you need Visual Studio 2010 for MVC3 or will it work in 2008?

  • Great....nice to hear

  • Cool! It is really getting better!

  • This is great! Love the new view engine.

    Has someone verified whether this works under Mono on Linux?

    Thanks!

  • Will you be fixing [ValidateInput(false)] to work with the new ASP.NET 4 input validation pipeline? Currently ValidateInput(false) is broken unless you set in the config.

  • Are you guys on meth?? MVC is just rocking!!

  • Scott, will future previews include improvments around Doughnut & Doughnut Hole caching?


  • I'm glad it's backwards compatible. These releases are coming really quickly.

  • Love the JSON binding!

  • Awesome, Scott!
    One question: how about source codes of MVC3 P1?

  • I see @RenderBody, but is there a way to render arbitralely code blocks?
    For instance, a certain view might need a specific javascript include, which should appear in the head of the html.

    // Ryan

  • Awesome set of improvements. Really like the JSON binding and DI improvements. Thanks!

  • Well done! Hope the final version will be out there sooner than March 2011?!?

  • It's very cool. Love new View engines and Dynamic ViewModel property.

  • Love most everything. Hate the dynamic ViewModel (at least so far). There's no type checking, so why make the syntax look like there is?

  • Any chance of sneaking a SkipFilter attribute in there?

  • Great job! Awesome job! A couple of questions, Scott: 1) Is there any intention of adding syntax highlight and intellisense for Razor on the IDE? 2) How can do the tooling aware of the view engines I have "installed" on my computer?

  • Congratulations on the release of MVC3. Looking forward to the final product.

  • Scott, these are great improvements! I really love how Microsoft keeps coming out with timely, incremental updates!

    Now, if only the MVC library properly supported the DLR languages, IronPython & IronRuby...

  • This feels like a 2.1 release, not a 3.0, why is the MVC team so eager to hike the version numbers so quickly, a 3.0 should be a huge shift in development compared to 2.0, I'm not seeing that, just minor incremental improvements (which are appreciated).

  • Thanks Scott,
    DBML support here? If e goes EDMX..

  • @Alan, Version 3.0 will sell more "Professional ASP.NET MVC 3" books by Guthrie et al. It should be 2.5 at most.

  • @Dan,

    >>>>>>>>>>> The changes all look great, Scott. But do you need Visual Studio 2010 for MVC3 or will it work in 2008?

    ASP.NET MVC 3 takes a dependency on .NET 4 (for a lot of the features I listed above), so you do need VS 2010 since that is the only version of VS that works with .NET 4. You can though use Visual Web Developer 2010 Express (which is free) if you don't have VS 2010 installed.

    Hope this helps,

    Scott

  • @Anon,

    >>>>>>>>> Has someone verified whether this works under Mono on Linux?

    It should work. I don't know of someone who has tried it yet - but it should work.

    Hope this helps,

    Scott

  • @korggy,

    >>>>>>>>> Will you be fixing [ValidateInput(false)] to work with the new ASP.NET 4 input validation pipeline? Currently ValidateInput(false) is broken unless you set in the config.

    Thanks for the suggestion - I'll forward it along to the team.

    Thanks,

    Scott

  • @lynn,

    >>>>>>>>>>> Scott, will future previews include improvments around Doughnut & Doughnut Hole caching?

    That is something we'll be looking at. Better caching helpers are on the list to make better.

    Hope this helps,

    Scott

  • @Vladimir,

    >>>>>>>>>> One question: how about source codes of MVC3 P1?

    I'm not sure whether it is available for download today - but it is definitely coming soon.

    Hope this helps,

    Scott

  • Great work !

    Can't wait for the colorization. The views looks so ... naked :-)

    I can't get the Ninject sample to work though.

    In NinjectServiceLocator.cs -> GetInstance(Type serviceType, string key) I get the following exception.

    Error activating IControllerFactory
    No matching bindings are available, and the type is not self-bindable.

    Am I missing something obvious here ?

    Linda



  • Really like that ViewData now makes use of the dynamic type.

  • @Ryan,

    >>>>>>>> I see @RenderBody, but is there a way to render arbitralely code blocks? For instance, a certain view might need a specific javascript include, which should appear in the head of the html.

    You can provide optional overrides and use the @RenderSection() helper to fill in other regions of the page. You could this approach to accomplish this I think.

    Thanks,

    Scott

  • @John,

    >>>>>>>> Any chance of sneaking a SkipFilter attribute in there?

    Can you send me email (scottgu@microsoft.com) with more details about this suggestion? I can then forward it onto the team.

    Thanks,

    Scott

  • @Allan,

    >>>>>>>> Great job! Awesome job! A couple of questions, Scott: 1) Is there any intention of adding syntax highlight and intellisense for Razor on the IDE?

    Yes - full code intellisense and colorization support will be provided in VS wit the next preview refresh.

    >>>>>>>> 2) How can do the tooling aware of the view engines I have "installed" on my computer?

    View engines can add appropriate templates into a specific folder which will cause them to show up within the Add->View dialog.

    Hope this helps,

    Scott

  • @Alan,

    >>>>>>>>> This feels like a 2.1 release, not a 3.0, why is the MVC team so eager to hike the version numbers so quickly, a 3.0 should be a huge shift in development compared to 2.0, I'm not seeing that, just minor incremental improvements (which are appreciated).

    We have even more features coming - today's refresh was just the first preview. :)

    Hope this helps,

    Scott

  • Hi Scott,

    Would it be possible to include also into dialog for create controller the template choosing? so that we could have our own templates like crud template, complicated crud template, empty controller template and such?

    Also, do you have plans to make better the current display and editor templates (e.g. include a dropdownlist template which takes data from viewdata, and selects the current item from viewdata model)?



    Thanks!

    Vladan Strigo

  • Good Job!

    the developer story continues to be one of the best stories comming from microsoft these days. We just transitioned our app to MVC2 and its paid off in spades. The razor engine and the next version of MVC look to improve what we have already.

  • Thanks Scott, been waiting for this release!

    If you want HTML colorization with .cshtml files in VS2010. Try these steps:
    1. Open Tools menu.
    2. Choose Options.
    3. Go to Text Editor, and third choice; File Extension.
    4. Enter cshtml as Extension and choose HTML Editor in the dropdown.
    5. Click Add and OK. Now try to open a .cshtml file – you should get HTML colorization.

    It sure helps on the eyes!

  • This sounds nice and all, but what "cool new features" is planned for the next webforms release.
    The 4.0 version have some nice features, like routing, but there were no real wow factor. It just felt more like a 2.5 version.
    So it would be nice if you could blog a little more about whats coming up for us not so trendy webform devs.

    Cheers

  • In the view engine dropdown, one of the options is ASPX (C#).
    Does that mean that there is no support for ASPX (VB)?

  • Can you define client side validation for IValidatableObject like you would for custom validation attributes?

  • Nice to see HTML5 doctype, but I would prefer lowercase version

  • @Vladan,

    >>>>>>>> Would it be possible to include also into dialog for create controller the template choosing? so that we could have our own templates like crud template, complicated crud template, empty controller template and such?

    That is definitely something we'd like to do - although we aren't sure just yet where it fits on the schedule.

    >>>>>>> Also, do you have plans to make better the current display and editor templates (e.g. include a dropdownlist template which takes data from viewdata, and selects the current item from viewdata model)?

    I'll kick off a thread with the team on that - I agree it would be great to try and make that easier.

    Thanks,

    Scott

  • @Andreas

    >>>>>> If you want HTML colorization with .cshtml files in VS2010. Try these steps:

    Good tip!

    Thanks,

    Scott

  • @dicoo,

    >>>>>>> This sounds nice and all, but what "cool new features" is planned for the next webforms release. The 4.0 version have some nice features, like routing, but there were no real wow factor. It just felt more like a 2.5 version. So it would be nice if you could blog a little more about whats coming up for us not so trendy webform devs.

    I'll definitely be blogging more about some cool WebForms features in the weeks ahead as well. Stay tuned.

    Thanks,

    Scott

  • @Socrates470BC

    >>>>>>>>> In the view engine dropdown, one of the options is ASPX (C#). Does that mean that there is no support for ASPX (VB)?

    The project I was in was a C# project - which was why only C# options showed up by default in the Add->View dialog. If you create a VB ASP.NET MVC project then you'll see the VB templates show up in place of the C# ones. VB is definitely supported for both .aspx and Razor templates.

    Hope this helps,

    Scott

  • Hi Scott,

    Are there any plans for having a class attribute that dictates which request it is bound from? For example, a search/maintenance form scenario, I might search by name (all get requests), I edit a record and the name of the posted record is now populating my search state object, even though the search state is still in the get request.

    Regards,

    Andrew

  • Great update. Congratulations.

    Can't get the JSON binding to work, though? Is there some sample code anywhere?

  • Great to see you are keeping up the momentum!

    Any news on the RESTful web services support that was trialled in MVC Futures? Also, what about razor supportting intellisense against an arbitary schema, not just HTML/CSS/JavaScript?

    Piers

  • Just spotted the comment regarding HTML Colorization in VS2010... using the "XML (Text) Editor" would give me what I want in terms of defining a schema... but what would be really cool would be if a Power Tool update for VS2010 allowed you to enter Extensions into that Tool/Options/Text Editor/File Extension dialog of the form xml.cshtml. Unfort. it doesn't like the additional period :-(

  • Will razor be available for mvc 2.0? Or only 3.0?

  • As said by Chris Scott I also not able to get the JSON binding to work. Do you have some sample?.

  • Wonderfull! ASP.NET MVC 2 is great, but with Razor and other features is Very Great!

  • Nice Work! the new view engine it's fantastic! and more easy to work with.

  • When are you going to support intellisense on razor views?

  • Tuesday, July 27, 2010 8:57 PM by Gene
    Will razor be available for mvc 2.0? Or only 3.0?
    ----------------------------------------------------------

    Will there be any final release of any of the new features (especially Razor) before 3.0?
    Amazing stuff btw, keep it up!

  • "oh no" - damn, another newer release haunts another...

    it's pretty hard to keep up with new technology changes in asp.net and around it (including mvc, wcf, linq, entity framework, odata and so on and on) if you (still) have to implement big web applications and have to realize new ordered projects.
    nowadays i think it could be a full time job itself just to keep up with all the new .net technologies by reading only a few .net team members blogs like yours, scott.
    hopefully the more evolving "convention over configuration" principle will make a lot of it easier very soon, including new APIs, versions, technologies and libraries.

    do you have any plans on finishing with your mvc 2 blog series: http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2.aspx ?

    you really should improve this website/blog - at least:
    - people should find blog series (MVC2; ASP.NET 4.0; ASP.NET 3.5; 3.5 SP1; Linq ...) more easyly ("categorie/blog series" links or something like that - the tag cloud isn't working very well for this)
    - add a published-date to every post

    thanks for blogging, tobi

  • Hi Scott,

    Firstly, Thanks for the update and great work from you and your team.

    Had an issue getting the new JSON binding to work. Had to register JsonValueProviderFactory manually within global.asax...

    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

    I may have missed something but this works.

    Thanks.

  • Hi Scott,
    do you know when the ASP.NET MVC 3.0 Final Release will be available?

    Thanks.

    Luigi

  • MVC 3.0 introduced already and it's been less than a year and half since MVC 1.0 was released!! Does this mean I have to upgrade my website again? MVC team I appreciate all that you guys are doing and these features look awesome... but can you give the community a chance to come to grips with all the features of one version before bringing out a major version upgrade? Every time it happens it makes a lot of our code obsolete!!

  • Is there any way of using a razor view from an HttpHandler instead of a controller action? I am working on a web application which needs some needs to send several rendered templates sequentially on the same HTTP Response (in a multipart/x-mixed-replace stream). Currently, I am using Spark to do this and I would like to switch to razor if I can.

    Is there any way i can do this?

    Thank you,
    George Steel

  • What about "@name" type of content? For example, throughout these comments, participants are saying stuff to each other using "@username" as notation, which is content. Will we have to use @"@"username or some other lame workaround? I suppose this would really be a corner case scenario since usually such content is data-driven / dynamic, not static.

  • Thanks for sharing such an awsome article on MVC .I am happy to download and install that immeditely.

  • I am about to release another version of NDjango editor extension with better intellisense support for strongly typed models. You made me think that it might make sense to provide an integration with ASP.NET MVC 3. I will need some information though - i.e. how can I have my template wizard access the model name selected in add view dialog. Any advice on where to get such information?

  • @Scott

    >>>>>>>> 2) How can do the tooling aware of the view engines I have "installed" on my computer?

    >>>>>>>>View engines can add appropriate templates into a specific folder which will cause them to show up within the Add->View dialog.

    What folder would be that?

  • Scott,

    Great article, as always. Between the dynamic viewdata, Razor view engine, improved DI support, and global action filters, this is starting to feel like the best parts of Rails + the best parts of .NET rolled tidily into one.

    One thing that felt a bit off & out of place to me is the PermanentRedirectXXX methods the team added to controllers. The permanentRedirectResult is a very useful result to have, in certain niche scenarios. It doesn't strike me as something that's as commonly useful as ViewResult, JsonResult, or regular RedirectResults. Since it's not likely to be as commonly used, it feels like you're bloating the controller w/ methods for no good reason, especially when you could accomplish the same thing by just adding a default parameter onto the existing redirect methods, something like isPermanent=false (since you're already dependant on 4.0...)

    Anyway, just my nitpick. Looks great!

  • Wonderful to see Asp .Net MVC 3 Preview 1 and Razor in action.

  • Scott, please please please do not forget to include the ability to return multiple views in mvc3 - I am hoping this will come in a future release? Without it, everything that mvc stands for is taken away. Thanks.

  • Great article.

    Thank you.

  • Coool :)
    Thank you.

  • I am still trying to learn MVC2 but MVC3 is already out....
    Scott take Vacation.......)

  • Wow... good job man..
    MVC3 will be just .NET4 ?

  • Am i missing something? where is the source code?

  • Hey Scott,

    How about a few inbuilt filters for helping users implement PRG pattern? I am sure it would be appreciate quite a lot (ViewData -> TempData and back... or any other recommended way)

    Regards,
    Karan

  • I vote for the donut caching and solid support for binding of non-consecutive collection indexes (not the hidden field with .index) for the next version..

  • Is there any plans to integrate the subcontroller concept into MVC 3.0? This was an MVCContrib addon for 1.0, wasn't available in 2.0 and isn't available in 3.0 either. I'm sorry, but Areas and Partial Views are just not the same thing.

    I know there's debates about whether a partial view is the same as a user control or not, but the surely it's a common requirement to share view AND controller logic across several different parent controllers?

  • I hope that it will support MEF as an option for DI. I realize it doesnt do things like a typical IoC would do, but there is some real potential for setting up the pieces to be as imports, and we simply setup our versions as Exports and if set a setting on the Global Code to tell it to use MEF, it simply pulls in the exports. Thoughts?

  • Looks very much similar to Classic ASP, can this be integrated with VS 2008 ?

  • Great stuff Scott. Any chance of posting this in VB for those of us who prefer a language syntax that doesn't resemble pig latin?

    Thanks!

  • Hi Scott,
    I have seen one of you video in which u said that ASP.net MVC is like Bike while
    ASP.net webforms can be considered as CAR. Diffference depends upon requirements
    of project and size perhaps.
    So, i basically wants to know that with this release of MVC , does it comes somewhat
    more closer to web forms in terms of capabilities.
    May be little awkward question but i think i cant get exact words to elaborate what
    really i want to ask.But i hope u got what i m trying to ask? :)

  • I know you've tried hard to gives the "dynamic" View (in the page) and ViewModel (in the controller action) the same name, but I think you should try harder. The disparity in names between "View" and "ViewModel" is a bit misleading and confusing, why not go with "ViewModel" in action and view.

    That's my only complaint after seeing all the cool stuff you've done. Can't wait to play with preview2

  • I think you should integrate T4MVC in asp.net mvc.

  • Good. My team is studing MVC2, I think it is significant to us to understand our direction.

  • Any plans to beef up test automation for mvc 3? its one area that rails still has the edge.

    gr8 work.

  • Scott, you develop faster than I can learn - I haven't finished Steve Sanderson's MVC 2 book yet! Actually I'm only reading that to concrete up my experience so keep it up and BRING IT ON!

  • I would ask that the display and editor templates are improved upon in MVC3. These have the potential to be really useful, but they're not really ready for the big time in MVC2 in my opinion. E.g. Better support for dropdowns in the editor, better support for editing complex objects.

  • What no Xml Action Result? I probably missed something, and I'm newish to it but in MVC2 I can auotgenerate JSON from a LINQ expression but not XML ?

  • This is looking great. I like the built-in JSON binding. This will definitely make my work easier in the future.

    Thank you for the great preview.

  • I would ask for ways to write controller logic without having hard dependency on the Httpcontext(May be with help of Dependency Injection). This will help to write clean controllers without any dependency on the httpContext, and helps in which in turn helps in writing more testable controllers.

    Thank you.

  • I like these enhancements, I live MVC, but I still like ASP.NET WebForms.

    What I really want is a mix.... I want to be able to create pages where:
    1. I have complete control over the markup and my markup could be literal javascript objects if that's what I want or standard html markup.
    2. Controls to produce markup or at least a framework that allows me to create them.
    3. I'm NOT tied to the FORMS model.
    4. Testability like MVC has.
    5. Friendly urls.
    6. Access to the same HttpContext across threads.
    7. Designer support.
    8. The ability to embed and use views (pages, user controls) from external assemblies (built into the framework).

    Good work, please keep it up and don't forget, enhancing the end developers ability to be productive AND in control is what really adds value to the .NET web technologies platform. After all, this was the premise of ASP.NET WebForms. Unfortunately, the constraints that were eventually evident were not fixed, we were just given yet another technology to learn with MVC but there is still something missing in my opinion.

  • Really it looks very much exciting. Razor will rock.

  • Hi Scott,

    Love the Razor engine. However, when used with Areas, VS2010 errors out when choosing the view engine. Also, how do you solve the situation below? I want the output to display num0, num1, & num2.

    @{
    for (var i = 0; i < 3; i++)
    {
    num@i

    }
    }

  • I figured it out.

    @{
    for (var i = 0; i < 3; i++)
    {
    num@(@i)

    }
    }

  • Hopefully you guys will manage to include intellisense support for razor in preview 2 :)

  • Cool new features!! I also like the new model validation options. It would be perfect if the annotations, inter atribute and inter object rules could be defined in a consistent way in the designer and then generated to code. Or even dynamically evalute linq expressions or wwf rules.

  • good stuff!!

  • Hi, Will you be fixing [ValidateInput(false)] to work with the new ASP.NET 4 input validation pipeline? Currently ValidateInput(false) is broken unless you set

  • Great job. Razor and MVC3 appears it will be a great.

  • Hello, I really like the new Razor engine, but I have a question, how can I import a namespace from inside a view??

  • Just upgraded an MVC2 app. Painful only begins to describe the experience. I wish dependency injection would just be added to MVC. There are too many options and too many out of date tutorials on it. If I wanted this kind of painful experience I'd use an open source stack. :( I spend more time trying to get that working then I do programming. Major hold up to getting my company to switch from WebForms. I like the direction you guys are going but I really don't want a million ways to do something. Give me a default one and allow for it to be replaced like you usually do.

    I'll probably be doing a blog post about getting my app upgraded. Until then ping me if you get these errors:
    No parameterless constructor defined for this object.

    Downloading the source to both Ninject and Ninject.Web.Mvc, upgrading them to .NET 4.0 and then recompiling is what worked for me. I couldn't get the NinjectServiceLocator in the sample provided to work. So I stuck with inheriting NinjectHttpApplication in global.asax.cs

  • @Christopher,

    >>>>>>> Just upgraded an MVC2 app. Painful only begins to describe the experience. I wish dependency injection would just be added to MVC. There are too many options and too many out of date tutorials on it. If I wanted this kind of painful experience I'd use an open source stack. :( I spend more time trying to get that working then I do programming. Major hold up to getting my company to switch from WebForms. I like the direction you guys are going but I really don't want a million ways to do something. Give me a default one and allow for it to be replaced like you usually do.

    You shouldn't have had to-do much to upgrade your application. What issue are you seeing?

    For future reference, you can use this tool to upgrade apps automatically: http://weblogs.asp.net/leftslipper/archive/2010/07/28/migrating-asp-net-mvc-2-applications-to-asp-net-mvc-3-preview-1.aspx

    We'll have better upgrade support built-into the box with later previews.

    Hope this helps,

    Scott

  • I recently came across something weird when trying to globalize an asp.net mvc 3 preview 1 application. Does this version not support Data Annotations from .NET 4.0? It seems like the DisplayAttribute is not being localized properly ( only using english resource file ). See further information here

    http://stackoverflow.com/questions/3511726/localizing-the-fields-attributes-on-the-accountmodel-for-asp-net-mvc

    will there be a future package like there was for ASP.NET MVC 2 whereas you could do:

    ModelMetadataProviders.Current = new DataAnnotations4ModelMetadataProvider();
    DataAnnotations4ModelValidatorProvider.RegisterProvider();

    To solve that problem?

    Thanks

  • Hi Scott,

    I wonder what is the reason to not to have REST URL?
    Instead of: /Store/UpdateProduct/ post
    This: /store/product put


  • I'm having issue after using the converter. My strongly typed views beleive their models are of type object and not the class passed in. Getting

    'object' does not contain a definition for ...

    Anyone else?

  • Any plan to add support for localization (similar to implicit localization in WebForm)/globalization, and theme?

  • Any plan to add support for localization (similar to implicit localization in WebForm)/globalization and theme?

  • Does the integrated DI support result in an abstraction of HttpContext (such as Request, Session, and Form )from the Controller?

  • This all looks really great! My most desired feature here, is to please allow the razor view engine to receive multiple variables, instead of just one called "Model". So, for example, in my controller I could say:
    List cats = Categories.find(all);
    List prods = Products.find(all);

    Then, in the view, I would have two variables, cat & prod, that could be referenced like so:
    @cats List
    @prods List

    Not being able to access variables in the view that are created in the model creates a feeling of isolation inside the view, as though its not an actual part of the app, but rather an add-on. Maybe this could be accomplished through some metadata in the controller that would mark variables as being available in the view like so:

    [ViewAccessible]
    List cats = Categories.find(all);
    [ViewAccessible]
    List prods = Products.find(all);

    Then, the variables wouldn't even need to be re-declared in the view, but they would just be directly accessible by name.

  • @Tyler,

    >>>>>>> This all looks really great! My most desired feature here, is to please allow the razor view engine to receive multiple variables, instead of just one called "Model".

    Unfortunately this is a little hard to do - since C#/VB doesn't currently allow dynamic properties except by referencing a variable of type dynamic. That is why you can do @View.Cats and @View.Prods within the razor view template, but not just @Cats and @Prods.

    Sorry!

    Scott

  • >> his all looks really great! My most desired feature here, is to please allow the razor view engine to receive multiple variables, instead of just one called "Model".

    Yeah, I second that. In ASP.NET WebForms you can easily pass several variables to the view.

    Here we have to create an enormous amount of ViewModels. Please implement this feature of multi variable passing!

    Thanks

  • Great post Scott, I'm really excited about this release.
    It would be great to see an examples of loading Razor templates from a database or separate assembly and also using the Razor parser independently - currently we are using NVelocity and object dictionaries to generate email templates - to swap this out for Razor and a dynamic view model would be sweet.

  • Hello Scott, and thank you so much for this exciting stuff!!

    I get an error when I try to run your sample files. The error is random and usually occurs when i run it the first time. The error message is: "Unable to complete operation. The supplied SqlConnection does not specify an initial catalog" I use VWD Express 2010. Any thoughts?

  • Awesome work ! I've just upgraded my socialnetwork lifebrixx from MVC 1.0 to 3.0, only small tweak I had to do was remove some of the page imports I'd done as it wouldn't compile.

    All up and running tho, converted any ViewData stuff to use the new dynamic ViewModel.

    Look forward to getting to grips with razor.

  • I am asking you - which software do you use to OPEN FILES on a website made in ASPX .CS MVC FRAMEWORK?

  • Exciting! Astonishingly good! MVC3 crosses five big items off my wish list. Thank you.

  • Looks great!

    Question for you: Currently there is an issue if I have defined a custom validation attribute and I set AllowMultiple = true. This allows me to use the same attribute (with different parameter values) for server-side validation. But the client-side validation code isn't generated properly. Only one entry (the last of the ones defined for a Field) is sent to the client (in the "Fields" array as part of the mvcClientValidationMetadata array).

    I'm hoping this gets fixed in MVC 3 (or before).
    Thanks!

  • Hi, Is there any plan to come up with a sample Multi Tenant application using MVC3?

  • How about this Scott -->
    Introduce a convention for adding extra .css and .js files to a view.
    whereby you would just do something like this at the top of your View.

    @Css.Add("jquery-ui.smoothness","tiny-mce");
    @Js.Add("jquery-ui","editor/tiny-mce");

    Then these extra files could be dynamically inserted at the bottom of the head section without having to create an extra ContentPlaceHolder in the head section of the master template or implement custom code to manage these extra additions. The convention would have to be where the css and Javascript is located in the directory structure similar to the Views convention.

  • Great Work so far -> good to see all the new improvements coming in to play.
    I think certain htmlhelpers could do with improving. Definitely dropdownlists - these take too long to set up at the moment for what they are as the IEnumerable needs setting up first which can get out of hand for lots of dropdowns. Also an easier way of handling checkboxlists would be good.

  • Hi, I dont know if some1 asked before, but what im looking forward the most is Caching, cos Caching in mvc 2 is VERY weak.

  • I can't seem to find any information on what's in the MVC 3 futures dll, anyone know?

Comments have been disabled for this content.