Asked  7 Months ago    Answers:  5   Viewed   334 times

I have an ASP.NET Web API (version 4) REST service where I need to pass an array of integers.

Here is my action method:

public IEnumerable<Category> GetCategories(int[] categoryIds){
// code to retrieve categories from database
}

And this is the URL that I have tried:

/Categories?categoryids=1,2,3,4

 Answers

55

You just need to add [FromUri] before parameter, looks like:

GetCategories([FromUri] int[] categoryIds)

And send request:

/Categories?categoryids=1&categoryids=2&categoryids=3 
Tuesday, June 1, 2021
 
Fredy
answered 7 Months ago
77

Web API doesn't have an out of box support with respect to web.config based enabling or disabling of helppage.

Some options you can consider:

  • Since HelpPage is installed as an MVC area, when deploying to production you could just exclude this HelpPage folder.

  • Create an action filter which returns 404 as suggested here: Conditionally disable ASP.NET MVC Controller

NOTE: for the above cases, if you are using the default Web API template, then yeah you would need additional step of display/not display the Help link from the navigation bar.

Monday, August 2, 2021
 
TheCarver
answered 4 Months ago
63

I think the best thing to do here would be to create a new class that implements Parcelable and pass instances of this new class between activities.

public class ParcelableListOfLists implements Parcelable {

    private ArrayList<ArrayList<imageHolder>> listOfLists;

    public ParcelableListOfLists(ArrayList<ArrayList<imageHolder>> listOfLists) {
        this.listOfLists = listOfLists;
    }

    public ArrayList<ArrayList<imageHolder>> getListOfLists() {
        return listOfLists;
    }

    // parcelable implementation here
}

Once you have this class, you can be in full control of how your data is parceled, and that lets you do some things that you won't be able to do with the Android built-in offerings.

Here's one way you could parcel a list of lists:

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (listOfLists != null) {
        dest.writeInt(listOfLists.size());

        for (ArrayList<imageHolder> list : listOfLists) {
            dest.writeTypedList(list);
        }
    } else {
        dest.writeInt(-1);
    }
}

And on the other side, you can recreate the list of lists like this:

public ParcelableListOfLists(Parcel in) {
    int size = in.readInt();

    if (size != -1) {
        this.listOfLists = new ArrayList<>(size);

        for (int i = 0; i < size; ++i) {
            ArrayList<imageHolder> list = in.createTypedArrayList(imageHolder.CREATOR);
            listOfLists.add(list);
        }
    } else {
        this.listOfLists = null;
    }
}

With all of this together, you can pass your list of lists between activities like this:

Intent nextActivity = new Intent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images", new ParcelableListOfLists(images));
startActivity(nextActivity);

and retrieve them in the next activity like this:

ParcelableListOfLists plol = getIntent().getParcelableExtra("images");
ArrayList<ArrayList<imageHolder>> images = plol.getListOfLists();
Wednesday, September 1, 2021
 
Aetherus
answered 3 Months ago
61

I prefer a variant through HTTP path variable for your problem, because of in REST ideology a resource ID is passed after a resource name 'http://../resource/id' and HTTP parameters are used for filtering.

Through HTTP parameters

If you need to pass your ids through HTTP parameters, see an axample below:

Here is your Spring MVC controller method:

@RequestMapping(value = "/books", params = "ids", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_params(@RequestParam List<Integer> ids) {
    return "ids=" + ids.toString();
}

And you can make a call using next variants:

  1. http://server:port/ctx/books?ids=5,65,42
  2. http://server:port/ctx/books?ids=5&ids=65&ids=42

Also take a look this discussion: https://stackoverflow.com/a/9547490/1881761


Through HTTP path variables

Also you can pass your ids through path variable, see an example below:

@RequestMapping(value = "/books/{ids}", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_pathVariable(@PathVariable List<Integer> ids) {
    return "ids=" + ids.toString();
}

And your call will be look like this: http://server:port/ctx/books/5,65,42


Wednesday, September 15, 2021
 
Narendra Singh
answered 3 Months ago
98

I like to put all my routes for the returned entity on the same controller. So a BooksController would look like this:

public sealed class BooksController : ApiController
{
    //Ideally this Repository would be a dependency injected object
    private readonly IRepository<Book> _repo = new BooksRepo(); 

    [Route("books")]
    [HttpGet]
    public IQueryable<Book> GetAll()
    {
        return _repo.GetAll();
    }

    [Route("books/{bookId:int}")]
    [HttpGet]
    public Book GetById(int bookId)
    {
        return _repo.GetById(bookId);
    }

    [Route("users/{userId:int}/books")]
    [HttpGet]
    public IQueryable<Book> GetBooksByUser(int userId)
    {
        return _repo.GetByUser(userId);
    }

    [Route("users/{userId:int}/books/{bookId:int}")]
    [HttpGet]
    public Book GetUsersBook(int userId, int bookId)
    {
        return _repo.GetByUser(userId).FirstOrDefault(book => book.Id == bookId);
    }
}

Now any route that returns Books should be on this controller. Any route that returns Users would be placed on the UsersController. This has helped us keep things organized and simplified since a Route attribute can be placed on any controller, you can very easily have any route defined on any controller that makes it difficult track down all the possible routes.

Saturday, November 13, 2021
 
Lime
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share