Wednesday, 20 May 2015

Controllers in MVC application

In this tutorial we will learn how to use controllers in an MVC application. previously  we created a simple application and we deployed that on IIS server and after we RUN that application , we saw that this application is running on local host and the link is .. http://localhost/FirstMvcApplication
Here controller name and method is stored in AppConfig- > Route Config class.
If we change this URL and make it like below it will give same output.
http://localhost/FirstMvcApplication/First/index


Here FirstMVCApplication is application name and “First” is the Controller name which is mention in Route class and “index”is the method name which also stored in route class.
This all route mapping stored in Global.aspx. when Basically Global.aspx file is used for handling application, objects,session events for ASP.NET web application which are running on IIS Server.
So when wewill see Global.aspx file have “Application_Start “ method.
In this method we will see a method for “RegisterRoutes() method.
RouteConfig.RegisterRoutes(RouteTable.Routes).
Go the definition of “RegisterRoutes()”  this method is define in “RouteConfing.cs” class. This method has Default controller and method.
Default controller method is “Home” but we change it as “First” and the default method name is Index.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace FirstMvcApplication
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "First", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}


Here you will see the ID which is optional now we want to pass some parameters to index method so change index method.
public string Index(string id)
        {
            return "Hi, your id is = " +id;
        }


 After writing this function now run your application and give the link and name also. Link is “http://localhost/FirstMvcApplication/first/Index/1”
Here i gave id as “1” and output  will be Hi, your id is = 1

We also can use query string for this
Now we will alter this function and will pass two parameters like following example
public string Index(string id, string name)
        {
            return "Hi, your id is = " +id+ " your name is = " +name;
        }


And type this URL and check the result
http://localhost/MVCDemo/home/index/1?name=Munesh

Just Like webform we can use query string
public string Index(string id)
{
    
return "Your Id = " + id + " and Name  is = " + Request.QueryString["name"];
} 


No comments:

Post a Comment

Paging in html table using javascript

I demonstrated how to use HTML tables on the client for a very simple client-side paging solution. I have heard from several people who p...