In this article we will see how to use a HTTP result in a controller in MVC.
IntroductionIn this article we will see how to use a HTTP result in a controller in MVC.Step 1Create a MVC project from the "Empty" template.Right-click on "Controllers" and select "Add" >> "Controller...".Step 2Select "MVC 5 Controller - Empty" to add an empty controller.Click on the "Add" button.Step 3Name the controller as in the following:And it will add an action result method to the controller.Step 4Now we need to create a view.Right-click on "Index" and select "Add View...".Step 5Name the view and select "Empty (without model)" as the template.Click on the "Add" button.Step 6Add a title to the index page.Step 7Create a httpstatuscoderesult method in the home controller, it returns a HTTP response status code and description. Here we return the "InternalServerError" status code. Step 8Create a link to call the httpStatusCodeResult method.Step 9Run the project, click on "Http Status Code" and you can see the defined message with HTTP response status code 500.0.Step 10Now let's see an example of HttpUnauthorizedResult.HttpUnauthorizedResult represents the result of an unauthorized HTTP request.Step 11Create a link to call the httpUnauthorizedResult method.Step 12Run the project, click on "Http Unauthorized" and you can see the defined message with HTTP unauthorized status code.Step 13So far we have seen HttpStatusCodeResult and HttpUnauthorizedResult, now let's see an example of HttpNotFoundResult.HttpNotFoundResult defines an object that is used to indicate that the requested result resource was not found.Step 14Create a link to call the httpNotFoundresult method.Step 15Run the project, click on "Http Not Found" and you can see the "File Not Found" message with HTTP response status code.
<< Day 13 >> Day 15
Diving Into ASP.NET WebAPI