Пример #1
0
 /// <summary>
 /// 启动webapi服务
 /// </summary>
 /// <param name="controllerNameSpace">分离式的controller</param>
 /// <param name="port"></param>
 public void Start(string controllerNameSpace, int port = 39654)
 {
     try
     {
         if (string.IsNullOrEmpty(controllerNameSpace))
         {
             AreaCollection.RegistAll();
         }
         else
         {
             AreaCollection.RegistAll(controllerNameSpace);
         }
     }
     catch (Exception ex)
     {
         throw new Exception("当前代码无任何Controller或者不符合MVC 命名规范! err:" + ex.Message);
     }
     try
     {
         httpServer.Start(port);
     }
     catch (Exception ex)
     {
         throw new Exception("当前端口已被其他程序占用,请更换端口再做尝试! err:" + ex.Message);
     }
 }
Пример #2
0
 /// <summary>
 /// 启动webapi服务
 /// </summary>
 public void Start(int port = 39654)
 {
     try
     {
         AreaCollection.RegistAll();
     }
     catch (Exception ex)
     {
         throw new Exception("当前代码无任何Controller或者不符合MVC 命名规范! err:" + ex.Message);
     }
     try
     {
         httpServer.Start(port);
     }
     catch (Exception ex)
     {
         throw new Exception("当前端口已被其他程序占用,请更换端口再做尝试! err:" + ex.Message);
     }
 }
Пример #3
0
        /// <summary>
        /// 获取映射
        /// </summary>
        /// <returns></returns>
        public static List <Routing> GetMapping()
        {
            Dictionary <string, Routing> dic = new Dictionary <string, Routing>();

            _areaCollection.RegistAll();

            var types = RouteTable.Types;

            foreach (var item in types)
            {
                var instance = Activator.CreateInstance(item);

                List <IFilter> iAttrs = null;

                //类上面的过滤
                var classAttrs = item.GetCustomAttributes(true);

                if (classAttrs != null && classAttrs.Length > 0)
                {
                    var actionAttrs = classAttrs.Where(b => b.GetType().BaseType.Name == ConstHelper.ACTIONFILTERATTRIBUTE).ToList().ConvertAll(b => b as IFilter);

                    if (actionAttrs != null && actionAttrs.Count > 0)
                    {
                        iAttrs = actionAttrs.OrderBy(b => b.Order).ToList();
                    }
                }

                var actions = item.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

                foreach (var action in actions)
                {
                    var getRouting = new Routing()
                    {
                        ControllerName = item.Name,
                        ActionName     = action.Name,
                        Instance       = (Controller)instance,
                        FilterAtrrs    = iAttrs,
                        Action         = action,
                        IsPost         = false
                    };

                    var postRouting = new Routing()
                    {
                        ControllerName = item.Name,
                        ActionName     = action.Name,
                        Instance       = (Controller)instance,
                        FilterAtrrs    = iAttrs,
                        Action         = action,
                        IsPost         = true
                    };

                    var pas = action.GetParameters();

                    if (pas != null && pas.Any())
                    {
                        getRouting.ParmaTypes  = new Dictionary <string, Type>();
                        postRouting.ParmaTypes = new Dictionary <string, Type>();
                        foreach (var pa in pas)
                        {
                            getRouting.ParmaTypes.Add(pa.Name, pa.ParameterType);
                            postRouting.ParmaTypes.Add(pa.Name, pa.ParameterType);
                        }
                    }


                    //action上面的过滤
                    var actionAttrs = action.GetCustomAttributes(true);

                    if (actionAttrs != null && actionAttrs.Length > 0)
                    {
                        var filterAttrs = actionAttrs.Where(b => b.GetType().BaseType.Name == ConstHelper.ACTIONFILTERATTRIBUTE).ToList();

                        if (filterAttrs != null && filterAttrs.Count > 0)
                        {
                            var ifilters = actionAttrs.ToList().ConvertAll(b => b as IFilter);

                            if (ifilters != null && ifilters.Any())
                            {
                                getRouting.ActionFilterAtrrs = postRouting.ActionFilterAtrrs = ifilters.OrderBy(b => b.Order).ToList();

                                var dPost = actionAttrs.Where(b => b.GetType().Name == ConstHelper.HTTPPOST).FirstOrDefault();
                                if (dPost != null)
                                {
                                    dic[postRouting.ControllerName + postRouting.ActionName + (postRouting.IsPost ? "POST" : "GET")] = postRouting;
                                }
                                var dGet = actionAttrs.Where(b => b.GetType().Name == ConstHelper.HTTPGET).FirstOrDefault();
                                if (dGet != null)
                                {
                                    dic[getRouting.ControllerName + getRouting.ActionName + (getRouting.IsPost ? "POST" : "GET")] = getRouting;
                                }
                            }
                            else
                            {
                                dic[getRouting.ControllerName + getRouting.ActionName + (getRouting.IsPost ? "POST" : "GET")]    = getRouting;
                                dic[postRouting.ControllerName + postRouting.ActionName + (postRouting.IsPost ? "POST" : "GET")] = postRouting;
                            }
                        }
                        else
                        {
                            dic[getRouting.ControllerName + getRouting.ActionName + (getRouting.IsPost ? "POST" : "GET")]    = getRouting;
                            dic[postRouting.ControllerName + postRouting.ActionName + (postRouting.IsPost ? "POST" : "GET")] = postRouting;
                        }
                    }
                    else
                    {
                        dic[getRouting.ControllerName + getRouting.ActionName + (getRouting.IsPost ? "POST" : "GET")]    = getRouting;
                        dic[postRouting.ControllerName + postRouting.ActionName + (postRouting.IsPost ? "POST" : "GET")] = postRouting;
                    }
                }
            }
            return(dic.Values.ToList());
        }