用AttributeRouting代替MVC(.net)默认路由

AttributeRouting for ASP.NET MVC lets you specify routes using attributes on your MVC controllers and actions.

To install AttributeRouting (ASP.NET MVC), run the following command in the Package Manager Console

PM> Install-Package AttributeRouting


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.


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() { /* ... */ }
} 

Attention VB.NET users! Get is a restricted keyword in the language, so to specify a GET request, you must enter: [[GET]("Some/Url")].



你可能感兴趣的:(用AttributeRouting代替MVC(.net)默认路由)