Continuing the article that has told you about how to produce output to the web page using View Action Method in ASP.NET MVC. Today, I will show you how to produce output to the web page from controller using Redirect Action Method.

Redirect ("url") Action Method

Redirect() action method when passed url as parameter redirect user to the url that is passed.

 public RedirectResult OutputRedirectView()
        {
            return Redirect("/controllerandaction/ReceiveWithRequestFormCollection");
        }

The above code will redirect user to "ReceiveWithRequestFormCollection" method of "controllerandaction" controller. Redirect() method returns RedirectResult action result that derives from ActionResult.

RedirectToRoute() Action Method

RedirectToRoute() action method when passed Route name as parameter redirects the user to that particular route defined in App_Start/RouteConfig.cs. This method returns RedirectToRouteResult action result that intern derive from ActionResult class.

public RedirectToRouteResult OutputToAction()
        {
            // return RedirectToAction("OutputPartialView");
            return RedirectToRoute("MyCustomRoute");
        }

Route Config

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


The parameters, if any defined in the route gets its default value, if not we need to pass 2nd parameter as route values.

return RedirectToRoute("MyCustomRoute", new { Id = 5 });

PartialView("_PartialViewName") action method

PartialView() action method when passed partial view name as parameter returns the Partial View as a web page.

public PartialViewResult OutputPartialView()
        {
            return PartialView("_MyPartialView");
        }


This method can accept 2nd parameter as model object to return along with the partial view so that this view can use Model to retrieve data. PartialView() method returns PartialViewResult action result that intern derives from ActionResult class.

That's the way to produce output in ASP.NET MVC using Redirect Action Method. Just look forward to further discussion in the next article.

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market s
egments.