1

I am new to ASP.Net programming. I have created an empty MVC controller called TestController, and as I follow the tutorial it says right click on the Index method of the Controller , then click Add View.

The View is successfully created and with I open the page

http://localhost:9993/Test

It successfully opens the relevant view file (Views->Test->Index.cshtml)

My question is where exactly in the code is the mapping defined that relates a View to a Controller ? Because when I open the controller, it has no information about which View file it relates to and Vice versa

Ahmed
  • 14,503
  • 22
  • 92
  • 150
  • possible duplicate of [how does asp.net mvc relate a view to a controller action?](http://stackoverflow.com/questions/9896950/how-does-asp-net-mvc-relate-a-view-to-a-controller-action) – jamesSampica Nov 27 '13 at 14:55

2 Answers2

3

In RouteConfig.cs (App_Start folder), your routes are defined. You can add or customize them there, in order to get to the right action in the right controller.

About views, it works by convention : if a controller is named TestController, and the action is named Index, it will search for an Index.cshtml view in a Views\Test folder (or Views\Shared if you wish to reuse a view across multiple controllers)

Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
  • I am up-voting this answer, but, it should also be noted that most of the "conventions" used in MS MVC can be overridden. For instance, in the example above, the Index action in the Test controller could also return a view with some other name, by specifying that name in the return of the action. It is quite powerful, and therefore quite complex and takes some getting used to. – KennyZ Nov 27 '13 at 15:07
0

By default ASP.NET MVC searches appropriate views in {controller} subfolder of Views folder, where {controller} is a name of ASP.NET MVC Controller class without word "Controller", and in Views/Shared folder.

Dmytro Rudenko
  • 2,524
  • 2
  • 13
  • 22