路由问题阻止从数据库显示图像

问题描述 投票:0回答:1

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

我在网上搜索并阅读了微软文档,但似乎无法弄清楚正确的路线。

此操作方法正常工作。请注意,没有{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方法不显示图像在浏览器中单击View Image时会生成以下路径... localhost:44353 / Blog / images / users / 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()
        });
    }

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

.net image model-view-controller routing core
1个回答
0
投票

我终于想通了!

我删除了所有踩踏PostDetail Route的路线,并添加了一条非常简单的路线来从网址中删除“Blog”。我不知道为什么我必须删除它的细节,但可以把它留在列表中(我怀疑我使用的.net核心版本中的一个错误是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()
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.