RedirectResult in MVC

RedirectResult represents redirection from an action method to other website. Action method send http status code 302 to browser, after receiving 302 response, browser sends another request to target URL. We use Redirect() method to create and return an object of RedirectResult class.

For example, from action method we want to redirect to facebook profile page of customer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Contact()
        {
            return View();
        }
       
        public ActionResult UserFBProfile(string username)
        {
            string fbURL = "https://facebook.com/"+username;
            return Redirect(fbURL);
        }
    }
}

In above code action method UserFBProfile() redirects to facebook profile page of user. Input parameter ‘username’ will hold customer’s profile name on Facebook.

Write below url in browser address bar and press enter key. You will see that facebook page will be loaded in browser. So from action method redirection happens and it will redirect to facebook.
http://localhost:53204/Home/UserFBProfile?username=test1

I have mentioned username test1 but you can try any actual name.

Leave a Comment

Your email address will not be published. Required fields are marked *