How To See All Registered Routes On Api Controller
Spider web API Routing
In the previous section, we learned that Spider web API tin be configured in WebApiConfig form. Here, we will learn how to configure Spider web API routes.
Web API routing is similar to ASP.Internet MVC Routing. It routes an incoming HTTP asking to a detail action method on a Web API controller.
Web API supports two types of routing:
- Convention-based Routing
- Aspect Routing
Convention-based Routing
In the convention-based routing, Web API uses route templates to determine which controller and activeness method to execute. At least 1 route template must be added into road tabular array in guild to handle various HTTP requests.
When we created Web API project using WebAPI template in the Create Spider web API Project section, it also added WebApiConfig class in the App_Start folder with default route every bit shown below.
public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Enable aspect routing config.MapHttpAttributeRoutes(); // Add default road using convention-based routing config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } In the in a higher place WebApiConfig.Register() method, config.MapHttpAttributeRoutes() enables attribute routing which nosotros will learn after in this section. The config.Routes is a route table or route collection of blazon HttpRouteCollection. The "DefaultApi" route is added in the route table using MapHttpRoute() extension method. The MapHttpRoute() extension method internally creates a new instance of IHttpRoute and adds it to an HttpRouteCollection. Even so, you can create a new route and add it into a collection manually as shown below.
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); // define route IHttpRoute defaultRoute = config.Routes.CreateRoute("api/{controller}/{id}", new { id = RouteParameter.Optional }, null); // Add together route config.Routes.Add("DefaultApi", defaultRoute); } } The following tabular array lists parameters of MapHttpRoute() method.
| Parameter | Description |
|---|---|
| name | Name of the route |
| routeTemplate | URL pattern of the route |
| defaults | An object parameter that includes default route values |
| constraints | Regex expression to specify characteristic of route values |
| handler | The handler to which the request volition be dispatched. |
Now, allow's see how Spider web API handles an incoming http asking and sends the response.
The post-obit is a sample HTTP GET request.
GET http://localhost:1234/api/values/ HTTP/1.ane User-Amanuensis: Fiddler Host: localhost: 60464 Content-Type: application/json
Considering the DefaultApi route configured in the above WebApiConfig course, the above request will execute Get() action method of the ValuesController considering HTTP method is a GET and URL is http://localhost:1234/api/values which matches with DefaultApi'due south route template /api/{controller}/{id} where value of {controller} will be ValuesController. Default route has specified id every bit an optional parameter so if an id is non present in the url and then {id} volition be ignored. The request'southward HTTP method is Go and then it volition execute Get() activity method of ValueController.
If Web API framework does non observe matched routes for an incoming request then it will send 404 mistake response.
The post-obit figure illustrates Web API Routing.
The following tabular array displays which action method and controller will exist executed on unlike incoming requests.
| Request URL | Request HTTP Method | Action method | Controller |
|---|---|---|---|
http://localhost:1234/api/class | GET | Get() | CourseController |
http://localhost:1234/api/product | Mail() | ProductController | |
http://localhost:1234/api/teacher | PUT | Put() | TeacherController |
Web API too supports routing same equally ASP.Internet MVC by including action method name in the URL.
Configure Multiple Routes
We configured a unmarried route to a higher place. Still, you can configure multiple routes in the Web API using HttpConfiguration object. The post-obit case demonstrates configuring multiple routes.
public static class WebApiConfig { public static void Annals(HttpConfiguration config) { config.MapHttpAttributeRoutes(); // school route config.Routes.MapHttpRoute( name: "School", routeTemplate: "api/myschool/{id}", defaults: new { controller="school", id = RouteParameter.Optional } constraints: new { id ="/d+" } ); // default road config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } In the to a higher place instance, School road is configured before DefaultApi route. And so any incoming request will be matched with the School route commencement and if incoming request url does not match with it then only it volition be matched with DefaultApi route. For example, request url is http://localhost:1234/api/myschool is matched with Schoolhouse road template, then it will be handled by SchoolController.
Notation: The reason to apply api in the road template is just to avert confusion betwixt MVC controller and Spider web API controller. You tin use whatsoever pattern based on your app compages.
Visit asp.cyberspace to learn about routing in item.
Attribute Routing
Aspect routing is supported in Web API two. As the name implies, attribute routing uses [Route()] attribute to define routes. The Route attribute can exist practical on any controller or activeness method.
In society to utilize attribute routing with Web API, it must exist enabled in WebApiConfig by calling config.MapHttpAttributeRoutes() method.
Consider the following case of attribute routing.
public class StudentController : ApiController { [Route("api/student/names")] public IEnumerable<string> Go() { return new string[] { "student1", "student2" }; } } In the above case, the Route attribute defines new route "api/student/names" which will be handled past the Get() action method of StudentController. Thus, an HTTP GET asking http://localhost:1234/api/student/names will return listing of educatee names.
Visit asp.net to learn about attribute routing in detail.
How To See All Registered Routes On Api Controller,
Source: https://www.tutorialsteacher.com/webapi/web-api-routing
Posted by: conleyaltoot.blogspot.com

0 Response to "How To See All Registered Routes On Api Controller"
Post a Comment