In this article, we are learning to map action for incoming Http URLs via defined routes.
How Routing Work?
The routing table is a collection of routes defined in the WebApiConfig.cs file and placed in the App_Start directory in the project. There you can also find one default route, which is created by default while creating a project.
Convention-based Routing
public static void Register(HttpConfiguration config) { // Enable attribute routing config.MapHttpAttributeRoutes(); // Add default route using convention-based routing config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
public static void Register(HttpConfiguration config) { // Enable attribute routing config.MapHttpAttributeRoutes(); // define route IHttpRoute defaultRoute = config.Routes.CreateRoute ( "api/{controller}/{id}", new{id = RouteParameter.Optional}, null); // Add route config.Routes.Add("DefaultApi", defaultRoute); } }
Verb | Usage | Example Url |
---|---|---|
GET | Used for getting data in a read only context. | localhost/users OR localhost/users/<user_name> for a specific resource (think get by id). |
POST | Used to create a new resource. | localhost/users/<user_name> with parameters of POST body containing parameters. |
PUT | Used to update an existing resource. | localhost/users/<user_name> with parameters of PUT body containing parameters. |
DELETE | Used to delete a resource. | localhost/users/<user_name> |
public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); // product route config.Routes.MapHttpRoute( name: "Product", routeTemplate: "api/getproduct/{id}", defaults: new { controller = "product", id = RouteParameter.Optional }, constraints: new { id = "/d+" } ); // default route config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
Attribute Routing
public class ProductController : ApiController { [Route("api/student/names")] public IEnumerable<string> Get() { return new string[] { "Laptop", "PC" }; } }
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular