Redirect Result in Controller Sample in MVC: Day 10

Introduction

In this article we will see how to use a redirect result in a controller in MVC.

Step 1

Create a MVC project from the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller...".



Step 2

Select "MVC 5 Controller - Empty" to add an empty controller.

Click on the "Add" button.



Step 3

Name the controller as in the following:



And it will add an action result method in the controller.



Step 4

Now we need to create a view.

Right-click on "Index" and select "Add View...".



Step 5

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.



Step 6

Redirect creates a redirect result object that redirects to the specific URL and redirect permanent returns an instance of a redirect result class with the permanent property set to true.



Step 7

Now we need to create a view.

Right-click on "redirect" and select "Add View...".



Step 8

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.



Step 9

Add a title to the about us page.



Step 10

Add two links, one is for the redirect and another is for a redirectpermanent as follows:



Step 11

Run the project and click on "About Us Redirect".



In the redirect case the about us page is rendered with http status code 302. You can find this using Firebug.

Step 12

Now click on "About Us Redirect Permanent".

In the redirect permanent case, the about us page is rendered with HTTP Status Code 30.

<< Day 9                                                      >> Day 11


Similar Articles