Redirect Result in Controller Sample in MVC

Redirect Result in Controller Sample in MVC

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...".

image.png

Step 2

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

Click on the "Add" button.

image.png

Step 3

Name the controller as in the following:

image.png

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

image.png

Step 4

Now we need to create a view.

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

image.png

Step 5

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

Click on the "Add" button.

image.png

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.

image.png

Step 7

Now we need to create a view.

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

image.png

Step 8

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

Click on the "Add" button.

image.png

Step 9

Add a title to the about us page.

image.png

Step 10

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

image.png

Step 11

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

image.png

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