Monday, April 21, 2014

Remove Ambiguity of Controller Names in MVC application

In this article I am sharing my thoughts on to create routing system in MVC application and terminology being used.

Here we go:

RouteData: This mechanism is used to examine an incoming URL and then decide which controller and action the request is need to be send.

Open your visual studio, click on New Project and select ASP.NET MVC3 Web Application:

clip_image002

Specify the name of your first application like your desire and click ok.

New window will appear ,from that window I picked up Intranet application whilst many options are there .Another intersting fact that there is a dropdown named as View Engine I seleccted Razor which is more specific to MVC 3 and MVC 4 and keep a checkbox “create a new unit test” unchecked.

clip_image004

Click on ok button .

The given below depict screen is default one comes after clicking on OK button.Hvaing multiple folder’s.

clip_image006

We need to make some amendments into this to understand how routing works. Application_Start method is responsible for registering the RegisterRoutes method . This Application_Start method is called by ASP.NET platform when the application is started fisrt.

To explore more on routing in MVC, Have a look into the link

http://www.c-sharpcorner.com/UploadFile/97fc7a/routing-in-mvc3-application/

Let’s see what happen when we run our application first time with default routing settings.

clip_image008

Which fulfills this route system?

clip_image010

Whenever we hit this url “http://localhost:60346/” ,First it jumps into the given class and ask for the action method names as “verify” as in depict image down level:

clip_image011

Let’s look into the image below to understand how we co-relate the namespaces of .cs file and routing path defined into global.asax.If you notice the red encircled box ,both pointing the same .namespace MVCSample.Controllers,It means whenever such url will hit by user having the same pattern defined in route will hit this class first.

clip_image013

If I remove namespce parameter from route then it throws the given below error:

clip_image015

This error comes because we’ve defined two controller with same name in a solution.Which rasie confusion which should be called here.

clip_image017

To over come on this issue we necessitated to place controller name with Complete namespace.

Here we define the route to resolve such issue as image shows:

clip_image019

After defining the route into an application runs in perfect mode :

clip_image020

If you hit the next url http://localhost:60346/Views/Register/RegisterAction ,it jumps into the different controller under MVCSample.Register namespace,

clip_image021

And gives you the result accordingly as below:

clip_image023

So using namespace parameter into routing made very much convenient for enduser to solve such issue.

Hope this will help you lot of geeks .Sample application been attached as reference.

Enjoy MVC and Routing.

Kindly inform me if having any query.

You can also touch base with me on Sachin Kalia Profile on C#Corner

Chees .Net Smile

Sachin Kalia

2 comments :

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. post article for ,if both action and controller names are same

    ReplyDelete