webapi mvc 基礎

標題  狀態  描述
WebAPI請求    http://www.cnblogs.com/babycool/p/3922738.html
 Media Formatters in ASP.NET Web API 2    http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
 原理    http://www.asp.net/posters/web-api/asp.net-web-api-poster.pdf
     http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
 

添加路由屬性

   
屬性路由能夠結合基於公約的路由。若要定義基於公約 》 的路線,請調用MapHttpRoute方法。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}有關配置 Web API 的詳細信息,請參閱配置 ASP.NET Web API 2.

注: 從 Web API 1 遷移
在 Web API 2,Web API 項目模板產生這樣的代碼:

protected void Application_Start()
{
    // WARNING - Not compatible with attribute routing.
    WebApiConfig.Register(GlobalConfiguration.Configuration);
}若是啓用了屬性的路由,則此代碼將引起異常。若是您升級現有的 Web API 項目以使用屬性路徑,請確保更新此配置代碼以下所示:

protected void Application_Start()
{
    // Pass a delegate to the Configure method.
    GlobalConfiguration.Configure(WebApiConfig.Register);
}更多的信息,請參閱配置 Web API 與 ASP.NET 託管.

添加路由屬性
這裏是一個示例使用屬性定義一條路線:

public class OrdersController : ApiController
{
    [Route("customers/{customerId}/orders")]
    [HttpGet]
    public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }
}
View Code

 

   

 http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-clienthtml

安裝 Web API 客戶端庫

Install-Package Microsoft.AspNet.WebApi.Clientweb

 
在 ASP.NET Web API 的 HTTP 消息處理程序
 

 DelegatingHandlerapi

 

 

你能夠向 a 的特定路由添加消息處理程序asp.net

 

 

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Route1",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "Route2",
            routeTemplate: "api2/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: null,
            handler: new MessageHandler2()  // per-route message handler
        );

        config.MessageHandlers.Add(new MessageHandler1());  // global message handler
    }
}
相關文章
相關標籤/搜索