【发布时间】: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