コード例 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            // 日志设置
            //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            // 使用认证
            app.UseAuthentication();
            // 使用MVC
            app.UseMvc();

            // 使用Swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            // 程序启动时,添加可能的示例数据
            // https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db
            using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var  dbContext  = serviceScope.ServiceProvider.GetService <DbContext>();
                bool hasCreated = dbContext.Database.EnsureCreated();
                if (hasCreated) // 仅在第一次创建数据库时添加样例数据
                {
                    SampleData.AddData(dbContext);
                }
            }
        }
コード例 #2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            View view = inflater.Inflate(Resource.Layout.Test, null);

            _addListButton     = view.FindViewById <Button>(Resource.Id.addListButton);
            _deleteListButton  = view.FindViewById <Button>(Resource.Id.deleteListButton);
            _addItemButton     = view.FindViewById <Button>(Resource.Id.addItemButton);
            _deleteItemButton  = view.FindViewById <Button>(Resource.Id.deleteItemButton);
            _getFromListButton = view.FindViewById <Button>(Resource.Id.getFromListButton);
            _getListButton     = view.FindViewById <Button>(Resource.Id.getListButton);
            _apiCallButton     = view.FindViewById <Button>(Resource.Id.callApi);

            _outputText = view.FindViewById <EditText>(Resource.Id.outputText);
            _nameText   = view.FindViewById <EditText>(Resource.Id.editTextName);
            _descText   = view.FindViewById <EditText>(Resource.Id.editTextDesc);
            _listIdText = view.FindViewById <EditText>(Resource.Id.editTextListId);
            _statusText = view.FindViewById <EditText>(Resource.Id.editTextStatus);
            _eanText    = view.FindViewById <EditText>(Resource.Id.editTextEan);
            _apiUrl     = view.FindViewById <EditText>(Resource.Id.editApiUrl);

            try
            {
                if (_repository.GetAllItemsWithCildren().Count == 0)
                {
                    SampleData.AddData();
                }
            }
            catch (Exception ex)
            {
                _outputText.Text += ex.Message;
            }

            _apiCallButton.Click += (sender, e) =>
            {
                try
                {
                    var url  = _apiUrl.Text;
                    var x    = new List <ItemModel>();
                    var json = "";

                    var request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                    // request.ContentType = "application/json";
                    request.Method = "GET";
                    Log.Debug("ApiCall", $"Request: {request}");
                    using (var response = request.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            json              = JsonValue.Load(stream).ToString();
                            _outputText.Text += json + System.Environment.NewLine;
                            Log.Debug("ApiCall", $"Response: {json}");
                        }
                    }

                    x = JsonConvert.DeserializeObject <List <ItemModel> >(json);
                }
                catch (Exception ex)
                {
                    Log.Debug("ApiCall", $"Exception: {ex}");

                    _outputText.Text += ex.ToString();
                }
            };

            _addItemButton.Click += (sender, e) =>
            {
                try
                {
                    var result =
                        _repository.Save <ItemModel>(new ItemModel()
                    {
                        Name         = _nameText.Text,
                        Description  = _descText.Text,
                        ListId       = int.Parse(_listIdText.Text),
                        KodEAN       = _eanText.Text,
                        Status       = _repository.GetItemStatus(int.Parse(_statusText.Text)),
                        ItemStatusId = int.Parse(_statusText.Text),
                    }).ToString() + System.Environment.NewLine;
                    _outputText.Text += result;
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
            };

            _deleteListButton.Click += (sender, e) =>
            {
                try
                {
                    _outputText.Text += _repository.Delete <ListModel>(int.Parse(_listIdText.Text)) + System.Environment.NewLine;
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
            };

            _deleteItemButton.Click += (sender, e) =>
            {
                try
                {
                    _outputText.Text += _repository.Delete <ItemModel>(int.Parse(_listIdText.Text)) + System.Environment.NewLine;
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
            };

            _addListButton.Click += (sender, e) =>
            {
                try
                {
                    _outputText.Text += _repository.Save <ListModel>(new ListModel()
                    {
                        Name         = _nameText.Text,
                        Description  = _descText.Text,
                        ListStatusId = int.Parse(_statusText.Text),
                        Status       = _repository.GetListStatus(int.Parse(_statusText.Text)),
                        Items        = new List <ItemModel>(),
                        CreateDate   = DateTime.Now.ToLongDateString(),
                        PersonId     = 1
                    }).ToString() + System.Environment.NewLine;
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
            };

            _getListButton.Click += (sender, e) =>
            {
                try
                {
                    _outputText.Text += _repository.GetList(int.Parse(_listIdText.Text)).ToString() + System.Environment.NewLine;
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
            };

            _getFromListButton.Click += (sender, e) =>
            {
                try
                {
                    var list = _repository.GetItemsFromList(int.Parse(_listIdText.Text));
                    _outputText.Text = "";
                    foreach (var item in list)
                    {
                        _outputText.Text += item.ToString() + System.Environment.NewLine;
                    }
                }
                catch (Exception ex)
                {
                    _outputText.Text += ex.Message;
                }
                //StartActivity(typeof(ItemListActivity));
            };
            return(view);
        }