ASP.NET MVC

Izi

Izi

Soldato
Joined
9 Dec 2007
Posts
2,718
So I have just watched the videos on ASP.NET MVC on the ASP.NET website and I'm excited.

I went from classic ASP to PHP to ASP.NET Web Forms. The learning curve to web forms was big (for me at least). With MVC it would appear you get the .NET amazingness with the control over HTML you had in ASP/PHP?

From what I understand there is no nasty viewstate, or ugly forms model. It appears to be the best of both worlds. There has to be some downsides though?

I assume the downsides are the face you have to build up your own controls because 'classic' ASP.NET forms controls no longer work properly without viewstate etc.

Anyone have much experience with it?
 
From what I remember all of the common controls work just fine.

All the MVC pattern really does is to help separate your model (i.e. database logic), view (HTML pages) and controllers (if the URL is /products/prod1 what page do I display and what data do I load into it) as it's name suggests.
 
so if you already properly structure your web applications in web forms (ui, biz logic, data access) then what are the other pluses of MVC?

It seems lots of opensource projects are moving to MVC, i read about it a lot and friends in the industry are telling me how great it is.

The immediate things which appeal to me are complete HTML control, but there has to be more.
 
Pho - I seem to remember you were using MVC, did you move away from it again?

what you using now?
 
I haven't done much work with web forms to be honest so I'm probably not the best person to answer how it all compares - I tend to just drop a report control in then code the backend for the report and its data :p.

I think the fact it separates the controller out more is really useful. So you'll end up with say a product controller class, with functions for adding/editing products. You can override these functions and redirect users to different views (HTML pages) given certain conditions.. it just gives you much greater control of how the website works and stops you having to code all this logic in individual aspx.cs pages which would become messy over time.

I still really like MVC so I'll be sticking with it. I do need to learn ASP.NET MVC more though too - it's on my big list of things to do.

May be worth a read:
http://hypertrends.com/net/2010/03/08/why-microsoft-asp-net-mvc-framework/
http://www.kevinwilliampang.com/2009/04/21/should-you-use-asp-net-mvc/


edit: here's something I pulled out of the template ASP.NET MVC files, the LogOn function (what I imagine gets called at some point after a form is posted to say http://127.0.0.1/Account/LogOn) in the AccountController class:

PHP:
[NonAction]
[AcceptVerbs(HttpVerbs.Post)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
	Justification = "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
{

	if (!ValidateLogOn(userName, password))
	{
		return View();
	}

	FormsAuth.SignIn(userName, rememberMe);
	if (!String.IsNullOrEmpty(returnUrl))
	{
		return Redirect(returnUrl);
	}
	else
	{
		return RedirectToAction("Index", "Home");
	}
}

If the user cannot be validated (bad username/password) it shows the logon page again (return View(); returns the default view for the LogOn function). If they provide a correct username/password and a return URL is specified then they are redirected to the return URL, else they are redirected back to the home page.

This is all done outside of logon.aspx.cs (in the web forms sense); there's no HTML involved in this file it's just controlling what happens in certain situations. But it's all in one place; you don't need to go hunting around in all the "aspx.cs" files to make changes to what happens, you can do it all in these controller classes.

I'm sure it also goes a long way to the open/closed principle - software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification - the logon files need not be touched once implemented because all of this logic is done at a higher level.

Maybe you decide that if the user is is connecting from a local IP address you display an alternative logon form (a weak example but an example all the same :p) your controller can switch the user to the new logon HTML without the old one being any the wiser, thus keeping is simple.
 
Last edited:
The M in MVC holds much, much more than just database logic. It (the pattern) should have every bit of logic, whilst the C does nothing more than control which model and view to invoke based on the input data (including environment, etc.), and the view does nothing more than present the resulting data from the model. :)
 
One of the key selling points of ASP.NET MVC that no-one has mentioned, is that the seperation of View from Controllers allows for much easier unit testing of your code.
 
Do go on... :confused:
Templates are old fashioned and archaic, whilst modular/meta markup generation is not, and removes the need for the developer to know the intricacies of XHTML (forced upon them by templating), whilst still affording them control of page content.

Templating also has the limitation of being too separated from the model.
 
Templates are old fashioned and archaic, whilst modular/meta markup generation is not, and removes the need for the developer to know the intricacies of XHTML (forced upon them by templating), whilst still affording them control of page content.

Templating also has the limitation of being too separated from the model.
You mean something like Haml?
 
No, that's segregating even further. :)

I mean something like DOM generator abstraction. Curiously, ASP.NET seems to be lacking in this field, which is perhaps not so surprising. Basically an abstracted API library of the XHTML DOM, so you can do something like (paraphrasing the excellent Seaside for Smalltalk framework's render canvas):
Code:
public void renderContentOn(RenderCanvas canvas)
{
  canvas.Div().Class("someclass").Value(() => canvas.Paragraph("Lorem Ipsum Dolo"));
  canvas.Div().Class("someOtherClass").Value(() => canvas.Anchor().Href("http://www.foo.com").Value("Click me!"));
}
 
I haven't seen anything like that done in MVC. What you actually build your DOM programmatically too, or do you have a barebones HTML file that you parse first?
 
You build it completely programmatically. Everything in one language (bar CSS).

No need to worry about semantics, as the framework takes care of it for you.

As for not seeing it done in MVC, do you mean "ASP.NET MVC" or are we talking the pattern MVC in it's true, generic term?
 
The latter. I've done something very similar for generating specific parts of views, but never for the whole lot. Do you know of any examples of large scale implementations like this?
 
Back
Top Bottom