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:
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.
Click on ok button .
The given below depict screen is default one comes after clicking on OK button.Hvaing multiple folder’s.
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.
Which fulfills this route system?
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:
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.
If I remove namespce parameter from route then it throws the given below error:
This error comes because we’ve defined two controller with same name in a solution.Which rasie confusion which should be called here.
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:
After defining the route into an application runs in perfect mode :
If you hit the next url http://localhost:60346/Views/Register/RegisterAction ,it jumps into the different controller under MVCSample.Register namespace,
And gives you the result accordingly as below:
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
Sachin Kalia
This comment has been removed by a blog administrator.
ReplyDeletepost article for ,if both action and controller names are same
ReplyDelete