Navigation is way of redirecting from one page to another. There are several way that I have explained below

1-HyperlinkIt is use with standard anchor tag which is use in html.

2-ActionLink- it is use to go to given action in the specific controller. If no controller is given then it will look for the action inside the current controller

Ex-1

Syntax-

@Html.ActionLink(text_to_display,actionName)
Use
@Html.ActionLink("greetuser","Welcome")
 
  
Ex-2
syntax
  @Html.ActionLink("text ","action","controller")
Use
  @Html.ActionLink("user login","Login","Acount")
 
Supplying querystring with Hyperlink
  @Html.ActionLink("user login","Login","Acount", new {id=1234}, null
)

 

3-RouteLink-It is the same as action link but it does not provide the facility for action name, controller name we have to specify it as follows:

Ex

@Html.RouteLink("Greetings", new {action="welcome"})
    @Html.RouteLink("Greetings",new {action="Welcome", Controller="account"})
4-response.redirect()-
  Response.Redirect("Controller/action")
        Response.Redirect("/action")

Generally written inside the controller or in the view

4-RedirectToAction()- This tells Mvc to redirect to specified action instead of rendering HTML or rendering a view

Ex

public ActionResult login()
    {
        return RedirectToAction("login2");
    }
    public void login2()
    {
        Response.Write("welcome");
    }

 

5-RedirectToRoute()

This function is used to link or navigate to the route defined by the user. This will be define in the routeConfig file.It will search for the registered route and then it will use the parameters defined in the route to navigat. This function can be useful for directly navigating to the different route apart from the route from where our home controller is.

The routes are defined in the routeConfig.cs file inside the RegisterRoutes function

Different ways of Navigation in MVC


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
 
namespace Demo
{                                             
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "MyRoute",//Route Name
                "Acount",//Url
                new {Controller="Account", action="Login"} //Parameter default
            );
 
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

 

Now inside the controller we do the following

public ActionResult Test()
    {
        return RedirectToRoute("MyRoute");
    }

Note-

1-Return View doesn’t make a new request and url in the browser’s address bar is updated with the generated url by mvc.

2-Return Redirect also makes a new requests and ur in the browser’s address bar is updated, but you have to specify the full url to redirect

3-Between RedirectToAction and Redirect best practice is to use redirectToAction for anything dealing with your application

  Modified On Mar-17-2018 03:17:43 AM
  1. Keep sharing these types of articles

    It was really helpful to read this post.

Leave Comment