• webapi学习


    在MVC中使用WebApi,初始化的时候App_Start文件夹中已经默认生成路由信息,不需要在RouteConfig中添加。

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

    Web API 框架默认是基于 Restful 架构模式的,与 ASP.NET MVC 有区别的是,它会根据 Http 请求的 HttpMethod(Get、Post、Put、Delete) 来在 Controller 中查找 Action,规则是:Action 名中是否以 Get、Post 开头?Action 上标记 HttpGet、HttpPost 等标记?并会完全忽视 Action 的方法名。所以我们一般在路由里面配置增加action的占位符{action},路由匹配规则和mvc一样。

    action方法的常用特性有以下几种,如果标记了请求类型或者action名中以put、delete开头那么httpclient请求的类型也必须用put、delete,否则无法匹配;另外我们可以用标记特性的方式修改路由匹配action。

    [System.Web.Mvc.HttpGet]
    [System.Web.Mvc.HttpPost]
    [System.Web.Mvc.HttpPut]
    [System.Web.Mvc.HttpDelete]
    [System.Web.Mvc.NonAction]
    [System.Web.Mvc.ActionName("myActionName")]

           //
            [System.Web.Mvc.HttpGet]
            public User GetByID(int id)
            {
                var user = _userList.FirstOrDefault(a => a.UserID == id);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return user;
            }
    
    
            //
            public User GetByName(string id)
            {
                var user = _userList.FirstOrDefault(a => a.UserName == id);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return user;
            }
    
            //
            public void Post([FromUri]User user)
            {
                
                if (user== null)
                {
                    throw new HttpRequestException();
                }
                _userList.Add(user);
            }
    
            //
            public void UpdateItem(int id,[FromUri]User user)
            {
                
            }
    
            //删除
            public void Delete(int id)
            {
                var user = _userList.FirstOrDefault(a => a.UserID == id);
                if (user == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                _userList.Remove(user);
            }
        }

    因为路由最后一项是{id},所以普通参数也必须id,和mvc路由匹配方式一样。mvc支持强类型,WebApi也可以。还可以指定参数来自uri还是body。

    [FromUri]表示请求参数在uri中

    http://localhost:53263/api/user/post?UserID=1&UserName=asdad&UserEmail=asdad&id=1

    [FromBody]表示参数在body中

        $.ajax({
                        url: "api/user/post",
                        type: "post",
                        data: {"UserID":1,"UserName":"asdas","UserEmail":"asdasd"},
                datatype:"json", success:
    function () { } });
  • 相关阅读:
    010-1 Socket地址族AddressFamily
    011 Socket定义客户端
    003 win7如何配置adb环境变量
    002 调试工具的具体功能
    001 Nibiru SDK 调试工具介绍
    001 UI介绍
    010 socket定义服务器
    001 Lua相关链接
    000 Lua目录
    深拷贝的、浅拷贝讲解以及示例
  • 原文地址:https://www.cnblogs.com/tgdjw/p/4626385.html
Copyright © 2020-2023  润新知