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

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

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

PM> Install-Package AttributeRouting code


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. ci


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")]. get

相關文章
相關標籤/搜索