Monday, February 16, 2015

MVC Custom Routing Simple Example

File ->New->Project
Note: Always select project when working with mvc (there is no option to work with file->new ->website) in MVC


Another Window will be displayed asking for the type of MVC Application we want to create

Select Project Template as


Internet Application




And Click on Ok Button


And Go to Controller Folder and Right Click  àAddController.




Write the following Code in Controller.

 public class Default1Controller : Controller

    {
       
        public ActionResult Index()
        {
            return Content("<h2> Index action </h2>");
        }

        public ActionResult register(String id)

        {
            return Content("<h2>" + id + " Registered Successfully </h2>");
        }

        public ActionResult ResultLogin(string uname, string pwd)

        {
            if (uname == "uday" && pwd == "uday")
                return Content("<h2> Valid </h2>");
            else
                return Content("<h2> Invalid </h2>");
        }
    }

Next





Write the Following Code in RouteConfig.cs

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

            routes.MapRoute(

                name: "Default",
                url: "{uname}/{pwd}",
                defaults: new { controller = "Default1", action = "ResultLogin", uname = UrlParameter.Optional, pwd = UrlParameter.Optional }
            );

            routes.MapRoute(

                name: "Default2",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default1", action = "Index", id = UrlParameter.Optional }
            );
        }
    }


Output:

Now, calling the Method register as follows






Now, calling the Method ResultLogin as follows


No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...