First play ASP.net MVC
(UPDATE: Cheers LiveWriter — Post to weblog as draft makes it print. Sorry for any mistakes in first try, this I’ve fixed them all)
In this post, I will discuss my experience while having a play with the ASP.net MVC framework. During the next series of articles, I will return over a number of the items discussed and dive into them to supply info. While it was still hot tonight, I just wanted to receive outside my thoughts.
But this brings me onto issue number one. The assemblies are required to be installed to the GAC! This is impossible with Shared Hosters (unless some hosters out there are prepared to put in CTPs on production servers). This evaluation websites! Guess I’ll just need to print the code. If anyone knows any hosters offering ASP.net MVC hosting, please leave a comment.
Once installed, you will have numerous templates inside your Visual Studio new job dialog
There are two Chief jobs, one is ASP.NET MVC Web Application and Evaluation with another being ASP.NET MVC Web Program. The first project includes the next project type (just the web app) that will setup the proper folder directory and give you the base template to fill . It will also create a MSTest job to begin composing your device tests.
After producing the projects. The layout is below.
A couple of things I wanted to point out. The content directory has the stylesheet for the site, this is really where images etc will go. The controller classes which will have all the methods for the application will be contained by the controllers folder. The versions will include any items related to storing your information. Views will contain the ASPX pages (and master pages).
The other thing you notice is that there’s a default.aspx from the main directory. It has the message if you start the file.
This makes sense, little hack but never mind. When we load the web site, then it will look just like below.
Great, now lets start having a play. Add a text box.
Like I added the control in code view, it didn’t automatically include a type tag for me. Easily fixed.
Strike F5 again and I got another exception.
System.NullReferenceException was unhandled by user code
Message=”Object reference not set to an instance of an object.”
This was since Visual Studio had pointed me into the full route to your ASP.net page (WebForms design — http://localhost:64701/Views/Home/Index.aspx). But with MVC this doesn’t work should I go directly to the origin of the website, as you browse the site via channels that are defined and it functions fine.
Now, the main thing I want to appear at first is the way to test the controller.
Testing the control
The first job I needed to do was delete the evaluation job and re-created it using a C# class library referencing the MbUnit.Framework assembly.
After I had it correctly building, I wished to check the Index activity on the test control. Here is the method called within our code and deals with what should occur when the site / indicator is seen. At the moment it does it tell the Index page.
Just to have an idea, I simply wrote the simple test below to find out what happened (and changed) if Index() was called.
public void IndexTest()
However, this throw an exception.
TestCase’HomeControllerTests.IndexTest’ failed: Object reference not set to an instance of the item.
Message: Object reference not set to an instance of an object.
Instant error. Of the methods on the Controller have the [ControllerAction] feature and a number of dependencies exist behind the scenes.
Now, I called Phil Haack’s article on Writing Unit Tests for Controller Actions (sounds just like what I am doing). What Phil describes would be to utilize a’Test subclass’. Using his approach, I change my test
public void IndexTest()
I then produce the HomeControllerTester which overrides the RenderView method and stores the name in a house.
Internal course HomeControllerTester: HomeController
Public series RenderViewAction get; private set;
Protected override void RenderView(string viewName, series masterName, object viewData)
I can then include a Assert.AreEqual(“Index”, controller.RenderViewAction); to guarantee the correct view is called. This is simple, but it is work just to confirm that the view is being called.
Another approach that he said was using Rhino Mocks, which I’ve used once or twice before in previous articles. It seems as if to mocking the RenderView method that support is brokenup. The strategy is to use the subclass above.
The next thing I wanted to test was creating data within the control and passing it into the view for outputting.
Inside my index method, I simply create a dummy generic list of strings that I wish to output.
public void Index()
List topics = new List();
topics.Add(“My String 1”);
topics.Add(“My String 2”);
RenderView(“Indicator”, subjects );
When calling RenderView, I pass into the generic list as an argument. Now, I just need to output the data. Within index.aspx, I added the following code snippet. This
But with this I got a compiler error message.
Compiler Error Message: CS0039: can’t convert type’System.Web.Mvc.ViewData’ into’System.Collections.Generic.List’ through a benchmark conversion, boxing conversion, unboxing conversion, wrap conversion, or null type conversion
Taking a look at the ViewData within the debug view, it has included a _data cube and every element within my listing is a separate item. I couldn’t find a way to extract that info as information that is _ is a member. The only method to extract the information is based on a title.
Inside my controller, I changed the code to set up the ViewData
ViewData[“Topics”] = issues;
I then changed my output code to be the next:
foreach(string s in ViewData[“Topics”] as System.Collections.Generic.List)
The data is then correctly outputted to the display.
The debug window then looks as I expect with _data comprising the generic list.
I’m guessing the ViewData constructor is used for something else or its a bug in the rendering. From the documentation, it does state we this is valid syntax:
List groups = northwind.GetCategories();
Strange! Moving on, talking thing is indeed .Net 1.1. I want to use objects. The very first thing I want to do is create the item
Public course TopicsViewData
Personal List topics = new List();
Public List Topics
Get return subjects;
This can store all my view info. I will then alter my Index code to use the ViewData item.
public void Index()
TopicsViewData viewData = new TopicsViewData();
viewData.Topics.Add(“My String 2”);
viewData.Topics.Add(“My String 4”);
I pass into the viewData object as the parameter, which works fine. Inside Index.aspx.cs (yes, MVC still has code behind, its just used less frequently ) I change what the webpage is inheriting from. In the previous edition, it inherited from ViewPage.
public partial class Index : ViewPage
However, to use a custom View Data thing we need to utilize the generic ViewPage and pass at the ViewData object.
public partial class Index : ViewPage
We are now able to get all the properties of TopicsViewData from within Index.aspx
Bit of additional work, but does make it a lot easier to utilize.
The MVCToolkit has the following (according to the readme):
“- Rendering helpers that make it easier to output various HTML tags in MVC Views
— Dynamic Data support: this provides ASP.NET MVC a more powerful and extensible scaffolding architecture. The toolkit also adds metadata pluggability, which permits the metadata used by Dynamic Data to use alternate stores (rather than the default feature based mechanism).”
One thing not mentioned is that it comprises the Blog sample program which utilizes the Dynamic Data support controllers. Subject for a different article…
There is a little bit of documentation over at http://quickstarts.asp.net/3-5-extensions/
In summary, this was just a very fast look at wjats om the CTP, the framework is still quite early in the evolution. A few of the concepts are there and its at the level I expected for thisw CTP. Worth looking at and using a play. I will post more as I find out things there.
One thing I’ve quickly realised is I have a great deal of work and reading to do to get up to speed using MVC, IoC and the way to other MVC frameworks (Monorail) interact. Don’t worry — I will be blogging my experience and learning.