コード例 #1
0
        public async Task <IActionResult> Post(ArticleAddViewModel request)
        {
            if (request == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(new MyUnprocessableEntityObjectResult(ModelState));
            }
            var article = _mapper.Map <ArticleAddViewModel, Article>(request);

            article.LastModified = DateTime.Now;
            article.Createdtime  = DateTime.Now;
            await _articleRepository.AddArticeAsync(article);

            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception("Save Faild!");
            }
            var viewModel = _mapper.Map <Article, ArticleAddViewModel>(article);
            var links     = CreateLinks(article.Id);
            var dic       = viewModel.ToDynamic() as IDictionary <string, object>;

            dic.Add("links", links);
            return(CreatedAtRoute("GetArticle", new { id = article.Id }, dic));
        }