zoukankan      html  css  js  c++  java
  • 设置WebAPI的路由与返回数据类型信息

    1、修改API路由

    在 项目中有个App_Start文件夹中的WebApiConfig.cs重新设定访问路由

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                //原来的默认路由
                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);
                //修改后的路由
                config.Routes.MapHttpRoute(
                    name: "ActionApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }
    修改路由

    在项目中Global.asax内引用WebApiConfig中设定好的路由

    public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    引用路由

    创建控制项目中的Controllers文件夹中新建控制器

    在新建好的控制器中定义方法

    /// <summary>
            /// 用户登陆
            /// </summary>
            /// <param name="postinfo"></param>
            /// <returns></returns>
            [HttpPost]
            public HttpResponseMessage UserLogin([FromBody]string postinfo)
            {
                  //方法内的代码
                  XXXXXXXXX
                 //返回json信息 下面会有定义返回形式,默认为XML方式返回
                  return ReturnHttpMessage.HttpMessage(JsonConvert.SerializeObject(user));
            }    
    定义方法

    PS:必须要在方法头部定义好[HttpPost]或者[HttpGet]、[HttpPut]、[HttpDELETE]的请求方式

    设定好路由后访问地址改变为:http://xxx(域名)/api/XXX(控制器)/XXXX(定义的方法)

    http://localhost:8035/api/Login/UserLogin

    2、修改设定的返回信息方式

    在控制器文件(Controllers)中添加个ReturnHttpMessage类

    /// <summary>
            /// 返回UTF8格式的json
            /// </summary>
            /// <param name="jsoninfo">json字符串</param>
            /// <returns></returns>
            public static HttpResponseMessage HttpMessage(string jsoninfo) { 
                return new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(jsoninfo, Encoding.UTF8, "text/json")
                    };
            }
    View Code
  • 相关阅读:
    闲来无事研究研究.Net中的异步编程
    Sql Server 因为触发器问题导致数据库更新报错“在触发器执行过程中引发了错误,批处理已中止”的问题处理
    c# 连接Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value:类型搞混弄出的错误
    VS2013 调试时出现“表达式计算器中发生内部错误”的问题解决办法
    WCF优化的几个常规思路
    UWP汉堡菜单
    C#注册系统全局快捷键
    CXF详细介绍
    hadoop默认3个核心配置文件说明
    在虚拟机配置hive
  • 原文地址:https://www.cnblogs.com/hyxf/p/5251594.html
Copyright © 2011-2022 走看看