Monday, 23 March 2015

MVC routing

MVC routing

MVC routing is the URL is mapped to controller and controller is mapped to any action type.
Suppose there is the control name customer where the action is  DisplayCustomer and you are checking in the local host than the URL will form as  http://localhost/ customer/ DisplayCustomer
Now lets see where the Routing configuration defined for MVC project.
You can see the below folder structure for Your MVC project and on it there is a file RouteConfig.cs







In Route configuration file you can find out the below code;
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

Name: Is Key name by which route will be identify, it should be unique name.
URL : It defined the kind of the URL you are connecting with. It will be the same of controller; e.g if you will see that customer is controller name where the action is  DisplayCustomer; it will be like; http://localhost/ customer/DisplayCustomer. If there is any value that you like to fetch suppose the Customer id is 110 than you need to pass as  http://localhost/ customer/DisplayCustomer/110.
You can provide the validation also to the routing that what number you are sending for action.

Default : It assign the default routing for your application. If you want to define the parameter as optional you can use the UrlParameter.Optional.
You can also set the default routing by below code on the existing routing.

routes.MapRoute("View1", "", new
            {
                controller = "Customer",
                action = "DisplayCustomer",
                id = UrlParameter.Optional
            }); // Parameter defaults

            routes.MapRoute("View", "Customer/DisplayCustomer", new
            {
                controller = "Customer",action = "DisplayCustomer",id = UrlParameter.Optional}); // Parameter defaults

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

So without giving any controller name it will redirect to the default controller it is given if you can see we have the controller and action as below;
public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        public ActionResult Index()
        {
            return View();
        }

        public ViewResult DisplayCustomer()
        {
            CustomerDetail objCustomer = new CustomerDetail();
            objCustomer.ID  = 12;
            objCustomer.Name  =  "Satya";
            objCustomer.Address  = "F311 Aditya Apt";

            return View("DisplayCustomer", objCustomer);
        }
       }

It will see the result in the page as like below, there you can observe there is no URL is displaying but it will opening the page;



MVC outbound URLs

Using of the Outbound we can redirect from one page to another directly that basically we are doing in the web pages.
Generally we are using anchor tag to move from one page to another like <a href=”home.aspx”>Home</a>. But as per MVC it is not the proper way you have to go through the controller only for any unbound URL.

So lets create an application. I will decorate you how it will work. Create three View;
Create a controller which is having three action like below;
public class WebsitelinksController : Controller
    {
        //
        // GET: /Websitelinks/
        public ActionResult Home()
        {
            return View("Home");
        }

        public ActionResult AboutUs()
        {
            return View("About");
        }

        public ActionResult ContactUs()
        {
            return View("ContactUs");
        }

       }

Create View for that controller as like below;


Now put the Hyperlinks for navigation on your view like below;
<h2>Home</h2>
<h3>Home Page</h3>

<a href="ContactUs">Contact US</a><br />
<a href="Aboutus">About us</a><br />

It will look as like and if you will click on contact page it will redirect to contact view.


1 comment:

Others

You will like to read it