webapi 初识 net

1.新建一个webapi 项目。

webapi 初识 net

2.新建筛选器文件,用户在接口执行前后进行特性操作。

public class MyActionWebApiAttribute : ActionFilterAttribute
    {
        public EnumHeader HeaderCode { set; get; }
        public override bool AllowMultiple
        {
            get
            {
                return false;
            }
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
            HttpResponseMessage response = actionExecutedContext.ActionContext.Response;
            try
            {
                response.Content.Headers.Clear();
                string HeaderCodeStr = "text/html";
                if (HeaderCode == EnumHeader.application_xml)
                {
                    HeaderCodeStr = "application/xml";
                }
                else if (HeaderCode == EnumHeader.application_json)
                {
                    HeaderCodeStr = "application/json";
                }
                response.Content.Headers.Add("Content-Type", HeaderCodeStr + "; charset=utf-8");
            }
            catch (Exception ex)
            {
            }
        }
    }//end
    public enum EnumHeader
    {
        text_html,
        application_json,
        application_xml
    }//end

重要点:

A)ActionFilterAttribute 必须要引入System.Web.Http.Filters;

B)

public override bool AllowMultiple
        {
            get
            {
                return false;
            }
        }

防止筛选器执行两遍。

C) HttpResponseMessage response = actionExecutedContext.ActionContext.Response;

 response.Content.Headers.Clear();

response.Content.Headers.Add("Content-Type", "text/html; charset=utf-8");

用于更改头文件。

3.在webapi中注册筛选器

public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Filters.Add(new MyActionWebApiAttribute());
            // 取消注释下面的代码行可对具有 IQueryable 或 IQueryable<T> 返回类型的操作启用查询支持。
            // 若要避免处理意外查询或恶意查询,请使用 QueryableAttribute 上的验证设置来验证传入查询。
            // 有关详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=279712//config.EnableQuerySupport();

            // 若要在应用程序中禁用跟踪,请注释掉或删除以下代码行
            // 有关详细信息,请参阅: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();
        }

重要点:

A)路径配置改为{action} 否则会有许多问题。

B)config.Filters.Add(new MyActionWebApiAttribute());

注册筛选器

4.使用

    [HttpPost]
        [MyActionWebApi(HeaderCode = EnumHeader.application_json)]
        public Site SiteList()
        {
            Site sites = new Site { SiteId = 1, Title = "test", Uri = "http://www.cnblogs.com/hcfan" };
            return sites;
        }
public class Site
    {
        public int SiteId { get; set; }
        public string Title { get; set; }
        public string Uri { get; set; }
    }

ok:success