Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API HTTP request naming convention

I would like to read about MVC4 WEB API's naming conventions, but I can't find any documentation about it. I would like to know if I can create endpoints with custom names and if so, how can I do that?

like image 280
Zoltan Varadi Avatar asked Apr 27 '12 12:04

Zoltan Varadi


People also ask

Should API routes be plural or singular?

Use plural form in naming resources in your path to avoid a mesh of singular and plural path variants for the same resource, which complicates the API implementation. Don't use verbs in naming your path resources, use plural nouns.

What URL pattern is used when writing a RESTful API?

A RESTful web service request contains: An Endpoint URL. An application implementing a RESTful API will define one or more URL endpoints with a domain, port, path, and/or query string — for example, https://mydomain/user/123?format=json .


2 Answers

The convention in MVC4 Web API is that the url starts with /api/ then has your controller name. From there, you don't specify the action name like you would normally do. The Controller Action is determined by the type of the request (GET, PUT, POST, DELETE). So you can create any end point that you want by creating a controller that inherits from API Controller.

The convention is; - Controller as your controller class name. - /api/ as the uri.

Let's say you want a tasks controller. Create a controller TasksController with a method Get(), then the uri would be /api/tasks.

The same goes for PUT, DELETE, and POST.

So again to answer your question... "I would like to know if i can create endpoints with custom names and if so, how can i do that?"

You can. Just create a controller that inherits from ApiController. You can name it whatever you want as log as it ends with Controller. ie ( MyController, TasksController, etc)

like image 121
Brett Allred Avatar answered Oct 25 '22 02:10

Brett Allred


If you want to understand how you can configure routes to your controllers actions (I believe this is what you mean by endpoints in this context), than you should read Routing in ASP.NET Web API article.

like image 35
tpeczek Avatar answered Oct 25 '22 04:10

tpeczek