ASP.Net Web API Action Result

falah mahmood

I'm building an ASP.Net Web API application and i have the following code...

public IHttpActionResult GetCustomers() {            
    var customers = context.Customers.ToList();
    return Ok(customers);
}

I'm using the Ok() method to return customers because i'm using an IHttpActionResult return type.

Now if i have the following method

public void DeleteCustomer(int id) {
  var customerInDb = context.Customers.SingleOrDefault(c => c.Id == id);

  if (customerInDb == null) {
      NotFound();
  }

  context.Customers.Remove(customerInDb);
  context.SaveChanges();
}

Can I use NotFound() method here when the return type of my ActionMethod is void???

Marco

Void does not have a return type. So you can try to call NotFound(), but I'm not sure if this would even compile - Haven't tried. Why don't you just go with an out of the box IHttpActionResult?

public IHttpActionResult DeleteCustomer(int id)
{
    var customerInDb = context.Customers.SingleOrDefault(c => c.Id == id);
    if (customerInDb == null)
    {
        return NotFound();
    }

    context.Customers.Remove(customerInDb);
    context.SaveChanges();

    return Ok(customerInDb);
}

Using IHttpActionResult is the more elegant version. If the id is invalid, you can just safely exit your method and tell the calling client that something went wrong. If everything went well, you're just giving the client a thumbs-up. IF you return your deleted entity or just an empty Ok() should not matter at this point.

Using void may or may not delete the entity in your data storage. The client would never know, because the server would not return any response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Web API read model inside action filter

Asp.net Web API Routing by action name fix

Can action parameter be a model in ASP.NET WEB API?

Get Url of current ASP.NET Web Api 2 action

ASP.NET Web API custom post action not working

Default controller action for Web API in Asp.Net MVC

Redirect from asp.net web api post action

ASP.Net Core Web API Controller action with many parameters

passing an array to a asp net core web api action method HttpGet

ASP.NET Web API Attribute Routing To Wrong Action

How to return json result on web api asp.net?

Mix web api action and non api action same controller asp.net core

How can I wrap failed model validation result of an ASP.NET Core 5 Web API controller action into another class and return response as OK

.net core asp.net web api action returning unexpected status code

How to deploy ASP.Net Web API (.Net 4.7.2) using github action

How to warp asp.net core api action result data by code&message&data without edit/add code in action

ASP.NET Core Web API multiple actions with the same action verb but different signature

ASP.NET Core Web API + react js, .AspNetCore.Session changes after each action

How to securely transfer data from Attribute to Action in asp-net Web API

ASP.NET Web API caches action filter attributes across requests

Can't map route to action, ASP.NET Core Web API

ASP.Net Web API 2 attribute-routed HTTP POST action not getting correct parameter

ASP.NET Web API generate url using Url.Action

ASP.NET Web API 2 Async action methods with Task.Run performance

Set action name as route attribute automatically in ASP.NET Web API 2

Route-specific ASP.NET MVC Web API message handler not invoking controller action

How do I disable automatic model binding for a specific ASP.NET Core 5.0 Web API action?

Why use async/await in an action in controller in Asp.net core web api

How to send controller action in url parameter in Asp.net Web Api?