Sabtu, 05 Oktober 2013

How to set Default action method in MVC.

Introduction

In MVC index method is the default method in application route file. Means you can say Index is the first running action method. Here this example show how to change default method means
Index to another method.

Follow some steps for changing Default method

Step-1: Create a new action method in the Home controller class
Like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public string detail()
        {
            return "hello";
        }

    }
}
This example take two method first one is Index ( Default method) and another one is detail method . If you want to set default method is detail then you would go RouteConfig.cs file.
Step-2:  Open RouteConfig.cs file which is inside in App_Start folder.
Step-3:  Change action value in RouteConfig.cs file
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "detail", id = UrlParameter.Optional }
            );
        }
Step-4: Assign second action method name (detail) to action value in RegisterRoutes method (which is mentioned above).

Step-5 : Save your Application and run.
How to set Default action method in MVC.

Tidak ada komentar:

Posting Komentar