Exemplo n.º 1
0
        // GET: Article/Details/5
        public ActionResult Details(int id)
        {
            ShowArticle         ViewModel = new ShowArticle();
            string              url       = "articledata/findarticle/" + id;
            HttpResponseMessage response  = client.GetAsync(url).Result;

            //Can catch the status code (200 OK, 301 REDIRECT), etc.

            if (response.IsSuccessStatusCode)
            {
                //Article goes in Data Transfer Object
                ArticleDto SelectedArticle = response.Content.ReadAsAsync <ArticleDto>().Result;
                ViewModel.article = SelectedArticle;


                url      = "articledata/findcountryforarticle/" + id;
                response = client.GetAsync(url).Result;
                CountryDto SelectedCountry = response.Content.ReadAsAsync <CountryDto>().Result;
                ViewModel.country = SelectedCountry;

                return(View(ViewModel));
            }
            else
            {
                return(RedirectToAction("Error"));
            }
        }
Exemplo n.º 2
0
        //在主页面上点击文章链接后显示,谁都可以查看
        public async Task <IActionResult> Show(string title)
        {
            //Console.WriteLine(title);
            //根据title在数据库中查找该article所有信息
            //封装Model返回页面
            var article    = _db.Articles.Where(a => a.Title == title).FirstOrDefault();
            var authorName = (await _userManager.FindByEmailAsync(article.Author)).UserName;
            var model      = new ShowArticle()
            {
                Title       = article.Title,
                AuthorName  = authorName,
                PublishTime = article.PublishTime
            };
            StringBuilder builder = new StringBuilder();
            string        tmp     = null;

            using (var reader = new StreamReader(article.URL))
            {
                while ((tmp = reader.ReadLine()) != null)
                {
                    //之所以加入\\n是因为这里的字符串是传给js,为了不让js中直接翻译换行,所以对\进行转义
                    builder.Append(tmp + "\\n");
                }
            }
            //Console.WriteLine(builder.ToString());
            model.Content = builder.ToString();
            ViewBag.Title = "Heyday-" + title;
            return(View(model));
        }