IHttpActionResult vs HttpResponseMessage

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Web API Text Articles and Slides

All ASP .NET Web API Videos

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In Web API 1, we have HttpResponseMessage type that a controller action method returns. A new type called "IHttpActionResult" is introduced in Web API 2 that can be returned from a controller action method. Instead of returning HttpResponseMessage from a controller action, we can now return IHttpActionResult. There are 2 main advantages of using the IHttpActionResult interface.

1. The code is cleaner and easier to read
2. Unit testing controller action methods is much simpler. We will discuss, how easy it is to unit test a method that returns IHttpActionResult instead of HttpResponseMessagein a later video.

Consider the following StudentsController. Notice both the Get() methods return HttpResponseMessage. To create the HttpResponseMessage, we either use CreateResponse() or CreateErrorResponse() methods of the Request object.

public class StudentsController : ApiController
{
static List[Student] students = new List[Student]()
{
new Student() { Id = 1, Name = "Tom" },
new Student() { Id = 2, Name = "Sam" },
new Student() { Id = 3, Name = "John" }
};

public HttpResponseMessage Get()
{
return Request.CreateResponse(students);
}

public HttpResponseMessage Get(int id)
{
var student = students.FirstOrDefault(s =] s.Id == id);
if(student == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student not found");
}

return Request.CreateResponse(student);
}
}

In the following example, we have replaced both instances of HttpResponseMessage with IHttpActionResult. To return status code 200, we used Ok() helper method and to return status code 404, we used NotFound() method. To the Ok() method we have passed the type we want to return from the action method. Also notice, the code is now much cleaner and simpler to read.

public class StudentsController : ApiController
{
static List[Student] students = new List[Student]()
{
new Student() { Id = 1, Name = "Tom" },
new Student() { Id = 2, Name = "Sam" },
new Student() { Id = 3, Name = "John" }
};

public IHttpActionResult Get()
{
return Ok(students);
}

public IHttpActionResult Get(int id)
{
var student = students.FirstOrDefault(s =] s.Id == id);
if(student == null)
{
return Content(HttpStatusCode.NotFound, "Student not found");
// return NotFound();
}

return Ok(student);
}
}

In addition to Ok() and NotFound() helper methods, we have the following methods that we can use depending on what we want to return from our controller action method. All these methods return a type, that implements IHttpActionResult interface.
BadRequest()
Conflict()
Created()
InternalServerError()
Redirect()
Unauthorized()
Рекомендации по теме
Комментарии
Автор

Thank you for this very clean and no nonsense instruction. It was a pleasure to see someone get right to the subject and not waste time. Namaste.

GoAway-ziwu
Автор

for those using .NET Core, IHttpActionResult is not supported, its a WebAPI thing, the way forward if you using .NET core is IActionResult.

yaghiyahbrenner
Автор

Superb. I love your learning style. You are doing a very good job helping freshers and experienced people as well. thank you venkat may god bless you.

saurabhchauhan
Автор

You've earned a subscriber. I like the way you teach, these videos are great for when I want to come back and refresh!
Thanks!

turboDout
Автор

@kudvenkat, You mentioned you were going to teach "unit testing controller action method". Which video are you referring to?

vishalpradhan
Автор

Thank you for this video sir. please we need a comprehensive video on knockout js. with web api. other online videos are not very understandable because some of them do not really understand knockout to the fullest.

streetzstudio
Автор

It's important to mentioned that CreateErrorMessage return serialized JSON content (with "Message" property), while Content return raw content.

spegase
Автор

Thanks for clarifying the differences between those. Provide videos on TDD for Web API 2 using Xunit and Nunit.

nuthanmurari
Автор

hey Venkat, what if we want to make the response be like TotalCount, TotalPages and Entity Details so if we used HttpResponseMessage or IHttpActionResult the response will be Entity Details and other details like StatusCode Header etc so where we can add TotalCount and TotalPages??

mamdouhemara
Автор

Extremely well explained. Answered and demonstrated this exact issue - wondering why the code I'm changing used httpActionResult when many for whatever reason recommend httpResponseMessage. In my case, action result is good.

GaryJohnWalker
Автор

Thanks for these valuable tutorial. You mentioned something about Unit test, Where I can find this unit test for controller ?

alwaseem
Автор

You search anything related to Web API or angular or C# on you tube believe me in first 3 links you will find Video of @Kudvenkat. Nice work and nice source of information. Thanks for sharing.

sanjaythorat
Автор

Well done! Informative and to the point.

JasonDimmick
Автор

Sir, What is Request object? Where this object got created and instanciated?

ilayarajaramasamy
Автор

Hello sir, may i know what is the default return type of api

minakshisutariya-fy
Автор

please give video on window service ....complete

gkmishra
Автор

Thank you. You helped me out once again!

dennisjansen
Автор

HTTP/1.1 504 Fiddler - Receive Failure

sovanjan