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-client

安装 Web API 客户端库

Install-Package Microsoft.AspNet.WebApi.Client

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

 DelegatingHandler

 

 

你可以向 a 的特定路由添加消息处理程序

 

 

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
    }
}

 

 

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
posted @ 2015-07-26 11:31  awp110  阅读(248)  评论(0编辑  收藏  举报