The setup and download was rather only and did not have any issues with the install.

thumbnail

First play with ASP.net MVC

(UPDATE: Cheers LiveWriter — Post to weblog as draft causes it to print. Sorry for any errors in first attempt, this I have fixed all)

In this post, I will discuss my experience while using a play with the ASP.net MVC framework. Over the next set of articles, I’ll go back over dip into these to supply more information and a number of the items. Tonight, I wanted to get my initial thoughts out while it was hot.

The setup and download was rather only and did not have any issues with the install

asp net hosting uk.

 

However, this brings me on issue number one. The assemblies are required to be installed to the GAC! This is hopeless with Shared Hosters (unless some hosters out there are willing to install CTPs on production servers).

asp net hosting uk

 This means I can’t deploy samples / evaluation sites until its RTM! I’ll just need to publish the code. If anyone knows any hosters that offer ASP.net MVC hosting, then please leave a comment.

Once installed, you’ll have a number of new templates within your Visual Studio new job dialog.

Picture

There are two main projects, one is ASP.NET MVC Web Application and Test with another being ASP.NET MVC Web Program. The first project contains the second project type (just the internet app) that will setup the correct folder directory and give you the base template to fill . It is going to also cause a MSTest job to start writing your unit tests.

After creating the jobs. The layout is below.

Image

A couple of things I wanted to point out. The content directory gets the stylesheet to the site, this is really where images etc will go. The controls folder will contain the controller classes which will have all the methods for your application. The models will include any items related to storing your data. Views will include the ASPX pages (and master pages).

Another thing you notice is that there’s a default.aspx in the main directory. If you open the document, it’s the message.

This is reasonable, little hack but not mind. Then it’ll look just like below, if the web site is loaded by us.

Picture

Great, now lets start having a play. First thing I want to do, add a text box.

Message=”Control’ctl00_MainContentPlaceHolder_TextBox1′ of type’TextBox’ must be placed inside a form tag with runat=server.”

Source=”System.Web”

ErrorCode=-2147467259

Like I added the control in code view, it didn’t automatically include a form tag for me. Easily fixed.

Strike F5 again and that I got another exception.

Message=”Object reference not set to an instance of an object.”

This was because Visual Studio had pointed me into the complete path for the ASP.net page (WebForms style — http://localhost:64701/Views/Home/Index.aspx). However this doesn’t work if I go directly to the origin of the site as you navigate the website via channels that are defined and it functions fine.

Now, the most important thing I want to appear at first is the way to examine the controller.

Testing the control

The first task I wanted to do was delete the evaluation project and re-created it using a C# class library referencing the MbUnit.Framework assembly.

After I had it properly building, I wished to test the Index activity on the test control. This is the primary method called within deals and our code with what should occur when the index is visited. At the moment it does it tell the Index page to render.

Just to have an idea, I just wrote the simple test below to see what happened (and altered ) when Index() was called.

[Test]

HomeController controller = new HomeController();

controller.Index();

But this throw an exception.

[failure] HomeControllerTests.IndexTest

System.NullReferenceException

Message: Object reference not set to an instance of an item.

Source: System.Web.Extensions

Instant error. Each of the methods on the Controller have the [ControllerAction] attribute and several of dependencies exist behind the scenes.

Now, I called Phil Haack’s article on Writing Unit Tests for Controller Actions (sounds like what I’m doing). What Phil describes is to utilize a’Exam subclass’. Using his approach, my test to interact with HomeControllerTester changes

public void IndexTest()

HomeControllerTester controller = new HomeControllerTester();

controller.Index();

then create the HomeControllerTester which overrides the RenderView method and stores the name in a property.

Inner class HomeControllerTester: HomeController

Public series RenderViewAction get; personal set;

Protected override void RenderView(string viewName, string masterName, object viewData)

I will then incorporate a Assert.AreEqual(“Index”, controller.RenderViewAction); to guarantee the right view is called. This is simple, but it is work simply to confirm that the view is being called.

Another strategy he said was using Rhino Mocks, which I’ve used once or twice before in previous articles. It appears like that service to mocking the RenderView method within this CTP is broken. The approach is to use the subclass above.

Outputting information

Next thing I wanted to test was generating data within the controller and passing it into the opinion for outputting.

Inside my indicator method, I simply create a dummy generic list of strings that I wish to output.

public void Index()

List subjects = new List();

RenderView(“Indicator”, topics);

When calling RenderView, I pass in the generic list as an argument. NowI only need to output the data. Inside index.aspx, I added the following code snippet. This

Outputting data

However, with this I have a compiler error message.

Compiler Error Message: CS0039: Cannot convert type’System.Web.Mvc.ViewData’ into’System.Collections.Generic.List’ through a benchmark conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Looking at the ViewData inside the debug view, it’s added a _data block and every element within my list is another item. But, I couldn’t find a way to extract that data as information that is _ is a private member. The only method relies on a name.

Image

Inside my control, I changed the code to set up the ViewData

ViewData[“Topics”] = issues;

I then shifted my output code to be the following:

foreach(string s in ViewData[“Topics”] as System.Collections.Generic.List)

The information is then correctly outputted to the display.

Picture

The debug window looks as I expect with _data comprising the generic list.

Image

I’m guessing the ViewData constructor is used for something else or its a bug in the rendering. In the instruction, it does state we this is valid syntax:

public void Categories()

List groups = northwind.GetCategories();

RenderView(“Groups”, groups );

Unusual! Moving on, talking thing is so .Net 1.1. I wish to work with objects that are strongly typed. The very first thing I Need to do is produce the item

Public class TopicsViewData

Private List topics = new List();

Public List Topics

Get return subjects;

This will store all of my view data. I will then change 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 3”);

viewData.Topics.Add(“My String 4”);

RenderView(“Indicator”, viewData);

I pass in the viewData thing as the parameter, which works good. Inside Index.aspx.cs (yes, MVC nevertheless has code behind, its just used less often) I alter what the page is inheriting from. In the previous version, it endured from ViewPage.

public partial class Index : ViewPage

However, to use a custom View Data thing we will need to use the generic ViewPage and pass at the ViewData object.

public partial class Index : ViewPage

We can now access all of the properties of TopicsViewData from inside Index.aspx

Bit of additional work, but does make it a great deal easier to utilize.

MVCToolkit

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 gives ASP.NET MVC a powerful and extensible scaffolding architecture. The toolkit also adds metadata pluggability, which allows the metadata used by Dynamic Data to utilize alternate stores (instead of the default attribute based mechanism).”

One factor not mentioned is that it includes the Blog sample program which utilizes the Dynamic Data support controllers. Subject for another post…

Documentation

There is a little bit of documentation over at http://quickstarts.asp.net/3-5-extensions/

Overview

In summary, this was only a very fast look at wjats om that the CTP, the framework is still quite early in the evolution. A few of the concepts are there and its own at the degree I expected for thisw CTP. Still worth looking at and having a play. I’ll post more as I find out stuff there.

One thing I have immediately realised is that I have a great deal of work and reading to do for up to speed using MVC, IoC and how to other MVC frameworks (Monorail) socialize. Do not worry — I will be blogging experience and my learning.

  

Back To Top