Wednesday, 20 May 2015

Action result And Action Method in MVC

Here we will learn about the action result and action method in Asp.net MVC.
Action Method :- This Action method is use to transfer the information means execute the response and give the output and generate the output.
By default it gives the output in”Action Result” form.
Will take an example for understanding this, When a end user type a url then “Asp.Net MVC life cycle” use route to create a route.config or we can say “Global.asax for getting the path of action method which to be executed.

Suppose a user enter URL like “http://dotnet-munesh.blogspot.in/”

When user run this url it execute action method which is specified in “Route.config” file  with as default action method and the default controller, For checking in “Route.config” file contain a method “RegistersRoutes” there we can check this.
If user type “http://dotnet-munesh.blogspot.in/Tutorial”

It means it execute default action method and a with “Tutorial” Controller.

If user type http://dotnet-munesh.blogspot.in/Tutorial/MVCTutorial

it means it execute MVCTutorial method from Tutorial controller.

If URL is ://dotnet-munesh.blogspot.in/Tutorial/MVCTutorial/1

Then here method is MVCTutorial and this method pass ID also.
So this is concept of MVC Action method.
Action Result :- Action Result is an abstract class which is define in “System.Web.MVC” namespace. Almost all the Action method return a object of the class which is derived from “ActionResult” class, that define the MVC Action method.
In MVC mostly Action method return a view which is the instance of “ViewResult” Class.
See following example where A Action method  is returning View
public ActionResult Index()
{
    return View();
}


We can also create Custom ActionResult return type using creating a class and by deriving “ActionResul”  abstract class.
Action result Return Type : - Action result in MVC is the return type for a controller method. This Action result return “File Streams”, Modal to view, and this also redirect to another  controller method.


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"];
} 


Create first Asp.Net MVC application

In this tutorial we will learn how to create a MVC project.
Asp.net MVC is basically an architectural  pattern , which allow us to create a dynamic website using HTML. This gives feature of "TDD development".
Following  some steps for creating your first Asp.net MVC projects.
1.   Open visual studio and click on
2.   File -> New -> Project
3.   Here you will see a screen from here select Web and ASP.NET MVC4 Web Application and give the name for this project and click OK.


4.   When we will click ok we will see Asp.Net New MVC project screen from here select “Empty template “ and select Razor as ViewEngine .
Razor And ASPX are type of ViewEngine.
Here we will prefer “VIEWeNGINE” because it is much clean and better in use.


5.   After click on OK ,It will create a MVC project. In Solution explorer we will see “Modal”, “view” , “controller” folder .
6.   Now go to controller folder and add a new controller to the project .
Right click on Controllers folder -> Select Add -> Controller


Give the name for this controller As FirstController and click on Add  and it will generate a file with the name FirstController .


Here when you will click on add , this will add a class “FirstController.cs” class.  it  will be available in controller folder. This class will be inherited by “Controller” Base class
Here this class will contain a method and the return type of this method is “ActionResult

7.   Add ActionMethod To the Controller class
ActionMethods are used for handling “user request” , validation of user input, execute code and the o/p.
MVC routing engine send the user request to the appropriate ActionMethod.
Now open “FirstController.cs” Class and add a ActionMethod and the give name it as “Index
The following code which is introduce in FirstController.cs Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FirstMvcApplication.Controllers
{
    public class FirstController : Controller
    {

        //
        // GET: /Home/
        public string Index()
        {
            return "Hello, from first MVC application";
        }

    }
}



There is a function “index” which will return String , now at this point if you will run this application it will deploy at local server and will give the output.

8.   Here in this condition if you will run this application it will give error.it is because if you will go to
App_Start -> RouteConfig.cs  class there is code which contain first which method and which controller class to be execute.

Here by default controller name is “Home” but we don’t have controller name with this name. So for execution this change the name of controller from Home to First.

Change this Home Controller to First controller For this code is below
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 }
            );
        }
    }
}


9.   Now Run your application it will output as "Hello, from first MVC application"

10.               At this time this application is running on local sever for deploying this application IIS sever there are some steps

Go to start -> press Run - > in Run window type “InetManager”
Or
Go to Control panel -> Administrative Tools - > search for Internet Information Services (IIS) Manager

Here if you are not able to see Internet Information Services (IIS) Manager then install it in your system. For this go to How To install IIS Server

11.               On IIS server screen you are not able to see your application (FirstMVCApplication ) so for this go back  to you application and right click on your application and go to properties click on WEB you will see there that you are using visual studio development server but instead of that we want to use local IIS web server so for this check redio button.

You remember that for creating you application on IIS server you have to open VS2010 as “Run as Administrator”.

Now click on Create virtual directory


12.               After creating virtual directory again go to IIS server refresh it then you will see there your application. It means now your application will run on IIS server.
  

13.              Now when you will run this application you will see the URL that itis running on “localHost/FirstMVCApplication/”
You will see the output “"Hello, from first MVC application"”


How to Install IIS 8 on Windows 8

Here we will learn how to install IIS server in window 8 for this there are some steps for this installing this.
1.   Go to Start menu and press CTRL+R and that window you type “appwiz.cpl”  click OK then you will see installation screen

2.   Means this will open features or installation part of control panel  of this screen in the left side you will see some links  and click on the “Turn Windows features on or off” link.


3.   When you will click on this link Windows features on or off screen will open from there you select “Internet information Service” check box


4.   When you will expand this you will see subcomponent you will see that it install all the things which we are needed for hosting a service.


5.   Here you will click on OK it will install this


6.   After this it will install so now again Go to Control panel -> Administrative Tools - > search for Internet Information Services (IIS) Manager

Now you will see this IIS Server.

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...