Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Sunday, November 23, 2008

ASP.NET MVC Routing Using XML Custom Configuration Settings

In ASP.NET MVC application, you need to add some routing code in the Application_Start of the Global.asax file to define your routing criteria. These lines of code take a shape of configuration settings for the MVC application and I was thinking if we can transfer these configurations line of codes to be XML settings in the web.config file. My target is to make the routing configuration in the web.config file as following:



As you can see, the later configurations define the Ignore and Routing lists of the MVC application. In the "Ignore" list, you define the URL criteria to be ignored by the MVC routing. In the "Map" list, you specify a list of the routes including the route name, controller and action. You can also define any optional parameter mappings for your actions.

To do that, I have to create a custom configuration section: MvcRouteConfigurationSectin. The section has a IgnoreCollection - inherited from ConfigurationElementCollection class - of IgnoreItem and another RoutingCollection of RouteItem.



The solution will also add an extension method for RouteCollection object so that you will be able to map the configuration section in the web.config directly to your RouteTable.



To use the project, you just need to include the MvcXmlRouting.dll in your MVC web project. Then you add the following line inside the configSection Tag in web.config:



You then define the ignore and the routing configurations for your web application as defined earlier in the web.config. Finally, you will need to edit the RegisterRoutes method in the Global.asax file to add a couple of lines as following:



Now all your routing configuration will go to the web.config file. No need to edit the Global file anymore. However, changing the routes in the web.config still need to reset your IIS as the routing is registered in Application_Start.

Download: Binary | Source Code

The project is implemented using ASP.NET MVC Beta 1 and Visual Studio 2008. It's not guaranteed that this project or the related assemblies will properly work in earlier or newer version of ASP.NET MVC Framework.


kick it on DotNetKicks.com
DZone - Vote Up!

Ping Back Links:
- http://blog.51mvc.com/view/20


Saturday, November 08, 2008

.NETWork.org 9th Gathering - Coming Out This November

.NETWork.org announced the next 9th Gathering to be held in 29th November. The event will be in CIC - Canadian International College and four speakers from ITWorx and Raya Software will be giving the sessions. Here is the list of the topics and speakers:

BizTalk - SharePoint Integration.
Hossam El-Deen M. Barakat
Senior Software Developer | Raya Software
Information Architecture
Mostafa Mourad
Team Leader | ITWorx

IIS 7
Hossam Kamel
Senior Software Engineer | ITWorx

Applying Domain Driven Design on ASP.NET MVC
Mohammed Meligy
Senior Software Developer | Raya Software









.network.org user group is a group of youth who share the same passion for the development on .Net platform in Egypt. The group started in the last December 2007 with a total of eight technical events during the last 10 months. For more information about the group, visit their website here.


PRG Pattern - You're Already Doing it

Have you ever had this message dialog asking about resubmiting the data when you try to refresh a web page? This actually happen after submitting a form or enter your login information.

One of the good practices when developing web application is to redirect the user after a successful posting request. This practice is refereed as PRG pattern (Post/Redirect/Get). The target is to avoid duplicate post requests from the client and providing a smooth navigation through the web application.

What actually happen in PRG is that the browser try to send an HTTP 303 redirect request along with HTTP "Location" header. It's nice to know that something you're used to make as a default practice is actually a pattern. The following is a sample code in ASP.NET MVC illustrating the usage of the pattern:

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword)
{
    if (ModelState.IsValid)
    {
        MembershipUser currentUser = Provider.GetUser(User.Identity.Name, true /* userIsOnline */);

        // Attempt to change password
        bool changeSuccessful = currentUser.ChangePassword(currentPassword, newPassword);

        if (changeSuccessful)
           return RedirectToAction("ChangePasswordSuccess"); //PRG Pattern recommendation
    }

    return View();
}


Monday, December 10, 2007

ASP.NET MVC Framework CTP is Finally Released.

ASP.NET MVC CTP is finally released. The framework is released as a part of new ASP.NET 3.5 Extensions package. The extensions includes some new stuff beside the MVC framewok like: The Entity Framework, new additions for ASP.NET AJAX and others.

Download ASP.NET 3.5 Extensions

Scott Guthrie published a series of articles about MVC. Here is the full list:

ASP.NET MVC Framework: Introduction
ASP.NET MVC Framework (Part2): URL Routing
ASP.NET MVC Framework (Part3): Passing ViewData From Controllers to Views
ASP.NET MVC Framework (Part4): Handling Form Edit and Post Scenarios


Tuesday, October 09, 2007

Finally MVC Model For ASP.NET

Finally we will have MVC model for ASP.NET. This was announced on Alt.Net Conference last day. In my opinion, this will be one of the most interesting feature added to ASP.NET. MVC is one of the old design patterns which introduced in many object-oriented languages. In MVC pattern, controllers just handle application flow, models represent the data, and the view is just concerned about presentation logic. The model was first introduced in SmallTalk. It is widely used on many other web frameworks like Zend Framework for PHP and MoneRail.

I was reading about Zend Framework from a while and I liked the way which they separate their pages and controllers classes. The model itself has a great trust from many web developers especially when building large scale projects where updatability is a critical issue.

Using MVC model, you will be able to define you application URLs like:
http://domain_name/Controller/Action/Parameter1/Parameter2/...

Where you map a URL to a certain Controller and action.

Read more about this topic here.
For more about MVC Model, check out this post