Saltar apartados

Controller

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…

Crear controlador

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.

Asignación del nombre del controlador

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

Código del controlador generado automáticamente

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.

namespace MvcUA.Controllers

{

    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

Probar aplicación

 

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

 Aviso de que no es posible probar aplicación en modo debug

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).

 Error en el navegador

If we added to the direction /ua (is not sensitive to capital letters and tiny), will have to visualise the text that have put

 Aplicación en el navegador

If we added /index will see that the result is the same

 Aplicación en el navegador con todos los parámetros

 

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.

 Generar solución para poder visualizar los cambios

Now if we wrote the direction in the browser, will see that you access to the new method.

 Navegar por listados

 

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

 Navegar con parámetros en la propia URL

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.

 Navegar con parámetros GET

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

 Navegar con parámetros GET - probando otra forma de llamada

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.

 Error que se puede producir al navegar con parámetros

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)

Tel: (+34) 96 590 3400

Fax: (+34) 96 590 3464

For further information: informacio@ua.es, and for enquiries on this web server: webmaster@ua.es

Carretera San Vicente del Raspeig s/n - 03690 San Vicente del Raspeig - Alicante - Tel. 96 590 3400 - Fax 96 590 3464