Microsoft’s new web framework goes under the microscope
When Microsoft launched the .Net Framework nine years ago, a major part of the offering was ASP.Net, a web application framework to replace Active Server Pages.
The great thing about ASP.Net was how it shielded the developer from the intricacies of web development.
You can create a form, add a button, double-click it to create an event handler, write some Visual Basic code, and it works.
It makes programming for the web almost like programming for Windows, yet still works across different browsers and platforms thanks to server-side controls.
Nearly 10 years on ASP.Net looks less clever.
Making web programming like Windows programming is no advantage to a new generation of developers for whom the web is primary.
While ASP.Net with web forms is still a capable platform, its disadvantages include that it:
1 Tends to mix program logic with presentation logic, since you write so much code at the page level. Windows Forms has the same issue.
2 Doesn’t help you create clean URLs that are search-engine friendly.
3 Relies on server controls that generate a lot of HTML, making it hard to control every aspect of page layout.
4 Doesn’t work well with unit test frameworks or test-driven development.
5 Doesn’t work well with the browser’s Back button.
The matter of clean URLs has a deeper importance.
There is a trend towards Rest (Representational State Transfer) in which applications are modelled as resources against which you can perform actions such as create, retrieve, update and delete, usually using HTTP and its verbs: POST, GET, PUT and DELETE.
ASP.Net web forms do not fit well with that model.
Introducing ASP.Net MVC
Rather than watch its web framework go out of fashion, Microsoft has created a
new one, which received its first full release at the
Mix09
conference in Las Vegas in March.
It is a free download but requires .Net Framework 3.5; however, you can use it with the free Visual Web Developer Express, with the latest serv ice pack.
MVC stands for Model, View, Controller and refers to a well-known development methodology in which three core parts of an application are kept separate:
1 The Model is the information the application presents.
2 The View is the user interface; or in this context, the web page.
3 The Controller handles events from the user interface and updates the Model.
Hands on with ASP.Net MVC
ASP.Net MVC is best learnt through use.
It’s very different from web forms.
To begin, install it and start a new project in Visual Studio 2008, choosing ASP.Net MVC Web Application.
If you have the full Visual Studio, you’ll be prompted to create a test project; say no.
When the project opens, there is a functioning skeleton application ready to run.
However, to understand it better, it’s best to delete much of what is generated.
Delete everything in the Controllers and Models folders, but not the folders.
Then delete everything in Views except for web.config.
Now the application will compile but not run.
If you look at the home page, Default.aspx, and its code-behind file Default.cs, you’ll see there’s some sort of redirection going on.
The code allows ASP.Net to process the request without serving an actual file that matches the requested URL.
Another clue about how this works is in Global.asax.cs, code that runs on application start-up. There you will find this code:
routes.MapRoute(
“Default”, // Route name
“{controller}/{action}/{id}”, // URL with parameters
new { controller = “Home”, action = “Index”, id = “” }
// Parameter defaults
);
This tells us incoming URLs are parsed into three components: a controller, for which the default value is ‘Home’; an action, for which the default value is ‘Index’; and an ID, for which the default value is empty.
Now it’s time to fix the project. Right-click the Controllers folder in the Solution Explorer and choose Add Controller.
By convention, Controller classes begin with the name and end with the word Controller.
This will be the Home controller, so call the new class HomeController. Visual Studio creates it with a single method:
public ActionResult Index()
{
return View();
}
Next, right-click the Views folder and add a new folder called Home. This will contain views for the Home controller.
Right-click Home and add a view called Index.
Now the project runs, although the home page is empty.
This works because ASP.Net MVC can now find a controller called Home and an action of Index, so runs the method above.
This returns a view that shares the same name as the method.
We have added a view called Index in the right location, so the project runs.
The ActionResult doesn’t have to return a view, or it could return a different view.
Try adding the methods in figure 1 to HomeController.cs.
Now run the project and experiment by modifying the URL. ‘http:// [yourhost]/’ works and returns a blank page. So does ‘http://[yourhost]/ Home/Index’ and ‘http://[yourhost]/ Home/DifferentView’.
The URL ‘http://[yourhost]/Home/NoView’ also works and returns Hello PCW!
What about the ID? If you create a controller method like this:
public ActionResultCheckID(string id)
then the ID value is passed to the method.
This value is the last part of the URL.
For example:
http://[yourhost]/Home/CheckID/Somevalue
would pass the string “Somevalue” to the CheckID method.
Related articles
Q.Why can't my browser find the website address I typed...
Q.All updates have been downloaded, so why won't Windows...
Q.How do I stop Windows 7 search?
Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...
|
|
|
|
|
Nikon Coolpix S570 BlackPrice: £66.99 |
Computeractive Ultimate Guide - Storage, Sharing & BackupPrice: £5.99 |
Back Issue CD-Rom 13 (2010)Price: £9.99 |
Hallmark Card Studio DeluxePrice: £15.31 |
Marine AquariumPrice: £15.41 |