May 14, 2013

MVCDemo1HelloWorld

                                        
Step1:

  Go to Visual studio and open new project and select Web and select ASP.NET   MVC4WebApplication and name it as MVCDemo1HelloWorld


  


Step2:


And you have to choose Empty/ Internet/ Intranet template depends on the requirement .I is going to choose empty now and have to select the view engine Aspx/Razor my choice is Razor because it is easy syntax





  

Step3:

once the project is created you will get the solution explorer with the below structure





Step4:

Go to Controller folder and open the HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDemo1HelloWorld.Controllers
{
    public class HomeController : Controller
    {
      

      //  my own method for displaying our message

  public string SayHello()
        {
            string message = "Step1 : Hello World! I am just one Controller with one Action, without View and Model<br>";

            message += "Step2 : You can also invoke me as <a href='http://localhost:51504/HelloWorld'> http://localhost:51504/HelloWorld</a> <br>";

            message += "Step3 : You can also invoke me as <a href='http://localhost:51504/HelloWorld/SayHello'> http://localhost:51504/HelloWorld/SayHello </a> <br>";

            message += "Step4 : Now go to Global.aspx.cs and see the methods, so that you will understan, how I was invoked.";

            return message;
        }
    }
}


Step5:

Run the project

Step6:

You will get an error like this




We will receive a 404 error. As per the defined routing, if there is no Action specified, it should redirect to the Index action inside the specified Controller. Here, our HomeController doesn’t have any Index action and throws an error.
So we need to set the routing or have to access the project using complete url
This is complete URL:  http://localhost:51504/Home/SayHello

Here Home is the Controller and SayHello is the method we have written in the controller to get output which will return message as string
So the output will be



If you need to get the output without giving the complete URL you need to set the things in RouteConfig.CS  this is called Routing.
Routing is one the best feature in MVC so we can customize with friendly urls name example HelloWorld

The RouteConfig.cs is looks like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCDemo1HelloWorld
{
    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 = "Home", action = "Index", id = UrlParameter.Optional });


          


        }
    }
}
Here the default controller is Home and action is Index so if u want to access your method you have to change in the routing
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "SayHello", id = UrlParameter.Optional });
So now you can run directly without giving the url because it is going to our method directly please find below




Lastly if you need friendly name HelloWorld we can set by adding additional route also
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                "HelloWorld",
                "HelloWorld/{action}",
                new { controller = "Home", action = "SayHello" }
                );

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


      


       After adding this you have to run and just type HelloWolrld  and you will get output
http://localhost:51504/HelloWorld

With this friendly Urls we can get security also because end user have not identify anything from HelloWorld internally which is referring home control

I hope this will give a good idea on MVC, happy coding folks

Please feel free to ask questions

2 comments: