ASP.NET MVC 2 RC

January 29, 2010 09:00 by bryan

MVC 2 is ready for use and designed to work with both VS 2008, as well as with VS 2010. 

Click here to download the ASP.NET MVC 2 release candidate for .NET 3.5 and VS 2008.  Nice thing is that it can be installed side-by-side with ASP.NET MVC on the same machine.


There is already an open DataReader associated with this Command which must be closed first.

November 18, 2009 10:25 by bryan

Having been using LINQ to SQL for some time now, I came across my very first issue "There is already an open DataReader associated with this Command which must be closed first." it's been some time since I had this issue.

The issue only appears on one database and that is being held on a SQL 2000 box.

The solution, sorry work around, is to transfer the results in to Lists using ToList() after each LINQ call.

I have found it hard to replicate in my test environment, as it only happens on a clients installation.

David Foderick posted a similar issue he had with the Entity Framework

After speaking with Eric Nelson, he pointed out that SQL 2000 has a lack of support for MARS, however there is no planning to provide support for this, however SQL Azure does not allow MARS. So perhaps this issue will get addressed some time in the future?


Videos of Scott’s sessions at the Manchester “Guathon”

October 26, 2009 08:41 by bryan

 

Mike Taulty of Microsoft has now sorted out the Videos from the event Scott spoke at: -

Visual Studio .NET

Silverlight

ASP.NET MVC

 

 


xVal 1.0 released

September 18, 2009 14:31 by bryan

An important tool when using MVC xVal version 1.0 is released, xVal is a validation helper for ASP.NET MVC that lets you use your own choice of server-side validation framework (e.g., Microsoft’s DataAnnotations attributes, or Castle Validator, or NHibernate Validaion) and dynamically generates client-side validation code from your rules.

xVal 1.0


ModelState is not Valid

September 18, 2009 14:18 by bryan

When you are using MVC you will come across using the ModelState, and the most common use is ModelState.IsValid, if you've not used this before then check out Validating Model Data in an MVC Application or xVal - a validation framework for ASP.NET MVC, what I did come across is an exception beyound the normal checks, that a table was missing from the database.  Although the error was recorded in the ModelState, it did not raise any exception and hence the exception handling did not capture the message.  To record the Exception you'll need to go over the collection and this code allows you to do this:

 

foreach (var value in ModelState.Values)

{

     foreach (var error in value.Errors )

     {

        throw new Exception (error.ErrorMessage, error.Exception);

     }

}

 

 


MVC 2 Preview 1

July 31, 2009 10:21 by bryan

The first of the next version of MVC 2 has been release by Microsoft, a few nice features, which has extended the Html helpers.

The full release will be shipped with Visual Studio 2010, and a version will be available for download for Visual Studio 2008 sp1 at the same time.

Why not watch it on Channel 9 - ASP.NET MVC 2 Preview 1 with Phil Haack and Virtual Scott

For more details check out Haacked 


ASP.NET MVC Futures

July 17, 2009 13:46 by bryan

Where are those extra features that did not quite make it in the initial release of ASP.MVC 1.0, they can be found on Code Plex, ASPNET

 

ASP.NET MVC Futures - Microsoft.Web.Mvc

The following are a few items in the ASP.NET MVC Futures assembly:

 

  • AsyncController
  • ButtonBuilder
  • Html.Substitute CacheExtension
  • FileCollectionModelBinder
  • LinqBinaryModelBinder
  • Html.Mailto Helpers
  • SkipBindingAttribute
  • RequireSslAttribute
  • AcceptAjaxAttribute
  • Html.RenderAction and Html.RenderRoute Extensions
  • Repeater Control
  • ByteArrayModelBinder
and more, for more details check out Maarten Balliauw Blog

 

 

Microsoft.Web.Mvc.dll (103.50 kb) 


Deploying MVC on IIS 6

July 2, 2009 12:06 by bryan

If and when you have to deploy your MVC application, you'll soon find out that IIS6 does not support Url Rewriting.  There are a number of options available to you, for a good list check out Steve Sanderson's Deploying ASP.NET MVC to IIS 6, the one I'm currently using is:

 

Use a wildcard mapping for aspnet_isapi.dll

This tells IIS 6 to process all requests using ASP.NET, so routing is always invoked, and there’s no problem. It’s dead easy to set up: open IIS manager, right-click your app, go to Properties, then Home Directory tab, then click Configuration. Under Wildcard application maps, click Insert (not Add, which is confusingly just above),  then enter C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll for “Executable”, and uncheck Verify that file exists.

Done! Routing now just behaves as it always did in VS2008’s built-in server.

Unfortunately, this also tells IIS to use ASP.NET to serve all requests, including for static files. It will work, because ASP.NET has a built-in DefaultHttpHandler that does it, but depending on what you do during the request, it might use StaticFileHandler to serve the request. StaticFileHandler is much less efficient than IIS natively. You see, it always reads the files from disk for every request, not caching them in memory. It doesn’t send Cache-Control headers that you might have configured in IIS, so browsers won’t cache it properly. It doesn’t do HTTP compression. However, if you can avoid interfering with the request, DefaultHttpHandler will pass control back to IIS for native processing, which is much better.

For small intranet applications, wildcard mappings are probably the best choice. Yes, it impacts performance slightly, but that might not be a problem for you. Perhaps you have better things to worry about.

For larger public internet applications, you may need a solution that delivers better performance.

Snippet taken from Steve Sanderson's Deploying ASP.NET MVC to IIS 6

 

For more information take a look at ScottGu's blogTip/Trick: Url Rewriting with ASP.NET

Also worth taking a look at Phil Haacked articles on ASP.NET MVC on IIS 6 Walkthrough


Asp.net MVC, Html.DropDownList and Selected Value

June 18, 2009 14:04 by bryan

I recently ran into the altogether common problem of the Html.DropDownList helper rendering a drop down list with no value selected. This is a major problem when editing data as by default, the first value is selected and saving would mean the first value is used.

There have been a few issues resulting in the same error. My issue was that I was setting the Name of the drop down list to be equal to the property on my model. I was using the Entity Framework, and had an Image class with a navigation property called Category. I was using this to render the ddl:

<%= Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewData["categories"])%>

In my controller, I was setting the ViewData like this:

this.ViewData["categories"] = new SelectList(db.CategorySet.ToList(), "CategoryId", "Title", img.CategoryReference.EntityKey);

Unfortunately, even though I had set the selected value (third parameter to the SelectList constructor), the ddl had no value selected.

The fix was quite simple:

<%= Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewData["categories"])%>

I just changed the Name of the drop down and handled the assignment in the controller.

The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.

 


Building up an MVC application

June 10, 2009 11:35 by bryan

When you open up MVC out of the box, you get the basic configuration, after a little playing around you soon see areas that require enhancing to, not only make your application more flexable, but also easier to maintain.

Here are a number of additional tasks you can perform that will help with your application.

Which IoC to use?

Don't worry, if you use the Common Service Locator it provides an adstraction over the IoC

How to validate?

I have already covered this one in a previous post, but I'd still go with xVal

Need to generate Themes?

I think it is so important to start correctly and generating the User Interface should be configurable, the easiest way of doing this is to implement a Theme, and Chris Pietschmann has do this for us, Implement Theme Folders using a Custom ViewEngine

Here is a copy of the source for safe keeping.

ASPNETMVC_Preview5_CustomThemeImplementation.zip (226.05 kb)

Need Ajax to work in MVC

Ajax.BeginForm and PartialViews

Extend the UrlHelper

Placing all your assets in one place is not only a good idea, but it also means you can be more flexable if you want to programmically change the theme of your user interface.

 

 

Which can be used like this:

Using Strongtypes and keep away from Strings

It appears so easy with MVC to use String everywhere, just DON'T, generate an extension for the UrlHelper

Now in your view it will look like this

Testing

I've been hit before by Microsofts in build Testing within Visual Studio 2008, great to have this feature built in, BUT, and a big but in order to run the tests you need to have Visual Studio install, so if you try and setup a Continuous Integration server you will also be required to install Visual Studio.

So best to go with what works well and that is nUnit, simple and easy, and more importantly loosely coupled

Mocking

With testing comes Mocking, and which framework do you use?  I also always say keep it simple and easy, so I'm going with Moq, short for "Mock-You", as this is the only Mock Framework that is built around .Net 3.5 and LINQ.