Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different ways for returning a view in ASP.NET MVC

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 5.26k
    Comment on it

    "Different ways for returning a view in ASP.NET MVC"

        In this article we are going to learn about different ways that can be used to return a View in Asp .Net MVC.

    There are different ways for returning a view in Asp .Net MVC such as:

    • return View()
    • return RedirectToAction()
    • return Redirect()
    • return RedirectToRoute()

    Let us discuss them in detail:-

    return View():

    This method is same like Server.Transfer() method used in Asp .Net WebForm. It is used to generate the HTML for a specific View on the browser.

    Example:

    public ActionResult Index()
    {
     return View();
    }
    

    We can also write the name of the view to be rendered.

    Example:

    public ActionResult Index(string Name)
    {
     return View("MyIndex");
    }
    
    public ActionResult MyIndex()
    {
     ViewBag.Message = "Hi, Dot Net Tricks";
     return View();
    }
    

    If we call the action method directly in the above action method like return MyIndex then also it will render the view of the MyIndex() as it will treat it as a method call.

    Example:

    public ActionResult Index(string Name)
    {
     return MyIndex();
    }
    
    public ActionResult MyIndex()
    {
     ViewBag.Message = "Hi, Dot Net Tricks";
     return View();
    }
    

    return RedirectToAction():

    This method is same like Response.Redirect() in Asp.Net WebForm. It helps to redirect to a particular action instead of rendering the HTML. It construct a redirect url to a specific action/controller in the application and generate the correct URL with the help of route table. This URL is then sent back to the browser as notification to make a new request for the specified action.

    Example:

    public ActionResult Index(string Name)
    {
     return RedirectToAction("MyIndex");
    }
    
    public ActionResult MyIndex()
    {
     return View();
    }
    

    Note:-> We can even call the action method inside some other controller.

    return Redirect():

    This method is also like Response.Redirect() in Asp.Net WebForm. It is same as RedirectToAction() the only difference is that it also construct a redirect url to a specific action/controller in the application but we have to generate the URL ourself, it is not generated with the help of the route table. The URL supplied is then sent to the browser as notification to make a new request for the specified action.

    Example:

    public ActionResult Index(string Name)
    {
     return Redirect("Home/MyIndex");
    }
    

    return RedirectToRoute():

    In this method we provide the name of the route defined in the Route.Config file. The provided route is then searched in the Route Table that is defined in global.asax. If the route is found then the view is redirected to that controller/action defined in that route.

    Example:

     public static void RegisterRoutes(RouteCollection routes)
        {
    
        routes.MapRoute(
        "MyRoute", // Route name
        "Account/", // URL
        new { controller = "Account", action = "Login"} // Parameter defaults
        );
    
        routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "MyIndex", id = UrlParameter.Optional } 
        );
    
        }
    

    The above code represents the Route.Config file that contains one custom route name "myRoute" and a Default route.

    Now we can use this route through return RedirectToRoute():

    Example:

    public ActionResult Index()
    {
    return View();
    }
    
    [HttpPost]
    public ActionResult Index(string Name)
    {
    return RedirectToRoute("MyRoute");
    }
    

    Hope it Helps.... Happy Coding..!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: