Create a controller and visualise it in the browser
The process to create a controller is so simple like putting on of the folder controller, press the right button, select Add and press on Controller…
It is necessary to indicate the name of the controller, respecting the end of the name so that it was Controller. We go him to call UA to our controller, with what the name remains like UAController.
The option to create all the methods as they are the creation, the erased, etc. We left it desmarcada.
It will generate us the following code
It creates us a new class and inside this an only method Index. This would be the method of access to our controller. Like comment indicates us as we will be able to access to our controller. It does not indicate at all of the action Index, because it is the method by defect and is not necessary to indicate it.
The method gives back a type ActionResult that has to generate it the call to View(). To see of a simple form as it works the call to controllers, go to replace it by string and go to give back really a chain.
public class UAController : Controller
// GET: /UA/
public string Index()
return "Access to the method by defect";
Now it is the moment to test it. If we pressed the button execute
It warns us that it is not configured to work in way depuración. We select the second option, to which can access with pressing Ctrl + F5
It will open us the browser by defect and if all work well, has to give you an error. This owe to that is looking for in the directory root /, that is resevado for a special controller designated Home (although afterwards will configure it so that it was the one who have created).
If we added to the direction /ua (is not sensitive to capital letters and tiny), will have to visualise the text that have put
If we added /index will see that the result is the same
Methods of a controller
To create new methods, will have to follow the same process that before. For example if we want to create a method that was listed and that it do not receive parameters, can follow the example of the previous method.
// GET: /UA/Listing
public string Listing()
return "Access to the method listed";
Before launching us to test it, it is necessary to clear a thing, now himself that is necessary to indicate the controller and afterwards the method. We put it like comment so that there are not confusions.
If we go back to the browser and put the new direction will give us an error, because before is necessary to generate the solution.
Now if we wrote the direction in the browser, will see that you access to the new method.
Step of parameters to the methods
The normal is that at least in the methods of edition or erased send a parameter, that can be the GO of the model with which work the controller.
In this case the simplest is to use the parameter by defect that offers us the surroundings that is not another than go,
// GET: /UA/Edit/5
public string Edit(int go)
return "Access to the method edit with the parameter go " to + go.ToString();
We generate the solution (do not forget you every time) and will see the result
The first that ask us is, ¿what has happened with the parameters by QueryString?. It does not be necessary to concern follows working the same, and can replace and access as up to now.
If we put the direction http://localhost:52314/UA/Edit/?Go=2, will see that the result is the same.
Even if we changed the call to the method and removed the parameter and read it from inside works equal.
// GET: /UA/Edit/?Go=1
public string Edit()
return "Access to the method edit with the parameter go " to + Request.QueryString["Go"].ToString();
The result is the same
Although now no longer we can use the first format of direction http://localhost:52314/UA/Edit/2, because it will not recognise it like element of the collection QueryString.
Computing service ASP.NET MVC 2 Framework
Universidad de Alicante Carretera de San Vicente del Raspeig s/n 03690 San Vicente del Raspeig Alicante (Spain)