Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an API for an ASP.NET MVC site with rate-limiting and caching

Recently, I've been very interested in APIs, specifically in how to create them. For the purpose of this question, let's say that I have created an ASP.NET MVC site that has some data on it; I want to create an API for this site.

I have multiple questions about this:

  1. What type of API should I create? I know that REST and oData APIs are very popular. What are the pros and cons of each, and how do I implement them? From what I understand so far, REST APIs with ASP.NET MVC would just be actions that return JSON instead of Views, and oData APIs are documented here.
  2. How do I handle writing? Reading from both API types is quite simple. However, writing is more complex. With the REST approach, I understand that I can use HTTP POST, but how do I implement authentication? Also, with oData, how does writing work in the first place?
  3. How do I implement basic rate-limiting and caching? From my past experience with APIs, these are very important things, so that the API server isn't overloaded. What's the best way to set these two things up?
  4. Can I get some sample code? Any code that relates to C# and ASP.NET MVC would be appreciated.

Thanks in advance!

While this is a broad question, I think it's not too broad... :)

There are some similar questions to this one that are about APIs, but I haven't found any that directly address the questions I outlined here.

like image 878
Maxim Zaslavsky Avatar asked May 23 '10 06:05

Maxim Zaslavsky


1 Answers

A REST service can return any media-type. It could be a standardized one listed at IANA, or it could be a custom one created by you.

OData is a protocol built on to of AtomPub. AtomPub itself is RESTful, however, OData currently breaks a few of the REST constraints.

Authentication of a RESTful service is best done using the HTTP Authorization header.

You write to an OData service the same way you do with an AtomPub service. Read the spec.

Personally, I would worry about writing a valuable service that delivers content efficiently before worrying about rate limiting. You can be happy when you finally run into that problem.

For more information on caching, read this.

like image 64
Darrel Miller Avatar answered Nov 07 '22 09:11

Darrel Miller