Пример #1
0
        public object Get(int id)
        {
            //使用unity框架
            IUserService service = ContainerFactory.BuildContainer().Resolve <IUserService>();

            return(service.Query(id));
        }
Пример #2
0
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务
            config.DependencyResolver = new UnityDependencyResolver(ContainerFactory.BuildContainer());

            //config.Filters.Add(new CustomBasicAuthorizeAttribute());//全局注册

            config.Filters.Add(new CustomExceptionFilterAttribute());                         //全局异常特性

            config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler()); //替换全局异常处理类

            //config.EnableCors(new EnableCorsAttribute("*", "*", "*"));//全部都允许 - 跨域请求 ,安装Cors

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "CustomApi",                               //默认的api路由
                routeTemplate: "api/{controller}/{action}/{id}", //正则规则,以api开头,第二个是控制器  第三个是参数
                defaults: new { id = RouteParameter.Optional }
                );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",//默认的api路由
            //    routeTemplate: "api/{controller}/{id}",//正则规则,以api开头,第二个是控制器  第三个是参数
            //    defaults: new { id = RouteParameter.Optional }
            //);
        }
Пример #3
0
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务
            config.DependencyResolver = new UnityDependencyResolver(ContainerFactory.BuildContainer());
            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
        }
Пример #4
0
 static void InnerRun()
 {
     Splash.Show();
     Logging.Initialise();
     ExceptionHandler.Attach();
     using (var container = ContainerFactory.BuildContainer())
     {
         container.Resolve <ChocolateyInstaller>().PatchRunNuget();
         container.Resolve <PendingRestart>().RemovePendingRestart();
         container.Resolve <AutoSubscriber>().SubScribe();
         var appBootstrapper = container.Resolve <AppBootstrapper>();
         appBootstrapper.Start();
         container.Resolve <IWindowManager>().ShowDialog(container.Resolve <ShellViewModel>());
     }
 }
Пример #5
0
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务

            #region Unity IOC容器初始化
            //IOC:config.DependencyResolver 换成自定义的Resolver
            config.DependencyResolver = new UnityDependencyResolver(ContainerFactory.BuildContainer());
            #endregion

            #region Filters过滤器注册
            //全局注册--所以WebApi请求都要做权限验证
            //config.Filters.Add(new BasicAuthorizeAttribute());
            //全局注册--所以的控制器和action方法都注册增加异常捕获
            config.Filters.Add(new ExceptionFilterAttribute());
            #endregion

            #region 全局异常驳回
            //替换系统默认的全局异常捕获,使用自定义全局异常捕获
            //config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
            #endregion

            #region 全局跨域设置(不建议)
            ////[EnableCors(origins: "http://localhost:9008/", headers: "*", methods: "GET,POST,PUT,DELETE")]
            ////第一个参数:允许的域名和端口;...具体EnableCorsAttribute参数配置看文档
            ////[EnableCors(origins: "http://localhost:9008/", headers: "*", methods: "GET,POST,PUT,DELETE")]
            //config.EnableCors(new EnableCorsAttribute("*", "*", "*")); //全部都允许,太粗暴了,不安全,建议针对单个action或控制器,比如:在action方法或者控制器上面添加-》[EnableCors(origins: "http://localhost:9008/", headers: "*", methods: "GET,POST,PUT,DELETE")]
            #endregion

            // Web API 路由---特性路由
            config.MapHttpAttributeRoutes();

            //需放在默认路由上面,不然会被默认路由先匹配到。
            //这里增加了一个类似mvc的路由,其实是违背了RESTful规则,不建议,这里只是为了方便测试
            config.Routes.MapHttpRoute(
                name: "CustomApi",                               //默认的api路由
                routeTemplate: "api/{controller}/{action}/{id}", //正则规则,以api开头,第二个是控制器  第三个是参数
                defaults: new { id = RouteParameter.Optional }
                );

            //默认路由
            config.Routes.MapHttpRoute(
                name: "DefaultApi",                     //默认的api路由
                routeTemplate: "api/{controller}/{id}", //正则规则,以api开头,第二天是控制器,第三个是参数
                defaults: new { id = RouteParameter.Optional }
                );
        }