【问题标题】:Routing Issue Prevents Display of Image from Database路由问题阻止显示数据库中的图像
【发布时间】:2019-04-20 15:30:33
【问题描述】:

我正在开发一个 MVC C# .net Core 应用程序。代码仍在进行中。我正在尝试在 SqlServer 数据库中显示从 TinyMce 存储的图像,而我的一条路线似乎在 url 中添加了一个额外的目录地址。所以,我认为这是一个路由问题,但我对其他想法持开放态度。

我搜索了网络并阅读了 microsoft 文档,但似乎无法找出正确的路线。

此操作方法可以正常工作。注意没有 {id} 参数。 在列表中正确显示图像。 在浏览器中单击查看图像时会产生以下路线... localhost:44353/images/users/MyImage.jpg

    [Route("/[controller]/PostList")]
    public IActionResult PostList(Guid tagId, Guid authorId, int numPerPage = 10)
    {
        ICollection<Post> posts;
        string currentTag = string.Empty;
        string currentAuthor = string.Empty;

        if (tagId == Guid.Empty && authorId == Guid.Empty)
        {
            posts = _blogRepository.GetRecentPosts(numPerPage).ToList();
            currentTag = "All posts";
            currentAuthor = "All authors";
        }
        else if (!(tagId == Guid.Empty) && authorId == Guid.Empty)
        {
            Tag tag = _tagRepository.GetAllTags().FirstOrDefault(t => t.Id == tagId);
            posts = _blogRepository.GetPostsByTag(tag).OrderBy(p => p.ModifiedDate).ToList();
            currentTag = tag.Content;
            currentAuthor = "All authors";
        }
        else if (!(authorId == Guid.Empty) && tagId == Guid.Empty)
        {
            Author author = _authorRepository.Authors.FirstOrDefault(a => a.Id == authorId);
            posts = _blogRepository.GetPostsByAuthor(author).OrderBy(p => p.ModifiedDate).ToList();
            currentTag = "All posts";
            currentAuthor = author.AuthorName;
        }
        else
            posts = _blogRepository.GetRecentPosts(numPerPage).ToList();

        return View(new PostListViewModel
        {
            CurrentTag = currentTag,
            CurrentAuthor = currentAuthor,
            Posts = posts,
            AllTags = _tagRepository.GetAllTags().ToList(),
            AllAuthors = _authorRepository.Authors.ToList()
        });
    }

此 Action 方法不显示图像 在浏览器中单击查看图像时会产生以下路线... 本地主机:44353/博客/图像/用户/MyImage.jpg 添加 {id} 显然会改变路线...... 我尝试了几种不同的路线都无济于事...

    [Route("/[controller]/PostDetail/{id}")]
    public IActionResult PostDetail(Guid id)
    {
        var post = _blogRepository.FindCurrentPost(id);
        if (post == null)
            return NotFound();

        return View(new PostDetailViewModel() {
            Post = post,
            AllAuthors = _authorRepository.Authors.ToList(),
            AllTags = _tagRepository.GetAllTags().ToList()
        });
    }

希望能够显示图像并拥有正确的路线。

【问题讨论】:

    标签: image routes asp.net-core-mvc


    【解决方案1】:

    我终于明白了!

    我删除了所有阻碍 PostDetail 路由的路由,并添加了一个非常简单的路由来从 url 中删除“博客”。我不知道为什么我必须将其删除以获取详细信息,但可以将其保留在列表中(我怀疑我正在使用的 .net core 版本中存在错误,即 2.0.9。但我真的不知道知道。

    这是从 URL 中删除控制器的路由(注意:仅在删除我首先为控制器定义的大多数其他路由后才起作用...)现在图像将显示出来。

        [Route("PostDetail/{id}")]
        public IActionResult PostDetail(Guid id)
        {
            var post = _blogRepository.FindCurrentPost(id);
            if (post == null)
                return NotFound();
    
            return View(new PostDetailViewModel() {
                Post = post,
                AllAuthors = _authorRepository.Authors.ToList(),
                AllTags = _tagRepository.GetAllTags().ToList()
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多