Attribute Routing

From Logic Wiki
Jump to: navigation, search


Attribute Routing

As it's described in detail in http://attributerouting.net/ it can be loaded to app via nuget

ASP.NET MVC

PM> Install-Package AttributeRouting

ASP.NET Web API

PM> Install-Package AttributeRouting.WebApi

Defining Routes

Routes are defined directly on actions using attributes. There are five such attributes: GET, which generates a URL that responds to GET and HEAD requests; POST, which generates a URL that responds to POST requests; PUT, which generates a URL that responds to PUT requests; DELETE, which generates a URL that responds to DELETE requests; and Route, which generates a URL that responds to all or only specified methods.

using AttributeRouting.Web.Http;
				
public class SampleController : Controller
{
    [GET("Sample")]
    public ActionResult Index() { /* ... */ }
                    
    [POST("Sample")]
    public ActionResult Create() { /* ... */ }
                    
    [PUT("Sample/{id}")]
    public ActionResult Update(int id) { /* ... */ }
                    
    [DELETE("Sample/{id}")]
    public string Destroy(int id) { /* ... */ }
    
    [Route("Sample/Any-Method-Will-Do")]
    public string Wildman() { /* ... */ }
}