C# 如何在.net core中显示图像
我正在使用.NETCore3.1 我已成功将图像上载到数据库中,但我正在尝试显示该图像,但无法显示 当我看到控制台错误时: 找不到此本地主机页找不到该网址的网页: https://localhost:00000000/home/WebRootPath/Imagef/_b9f5Image.jpg 学生会C# 如何在.net core中显示图像,c#,asp.net-core,C#,Asp.net Core,我正在使用.NETCore3.1 我已成功将图像上载到数据库中,但我正在尝试显示该图像,但无法显示 当我看到控制台错误时: 找不到此本地主机页找不到该网址的网页: https://localhost:00000000/home/WebRootPath/Imagef/_b9f5Image.jpg 学生会 namespace TestCore.Models { public partial class Student { public string ImageD
namespace TestCore.Models
{
public partial class Student
{
public string ImageDescription { set; get; }
public string ImageName { set; get; }
}
public class StudentViewModel
{
public string ImageDescription { set; get; }
public IFormFile File { set; get; }
}
}
HomeController.cs
[HttpGet]
public IActionResult Index()
{
return View(studDBContext.students.ToList());
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(StudentViewModel svm)
{
if (svm.File != null)
{
var fileName = Path.GetFileName(svm.File.FileName);
var createGUID = "_" + Guid.NewGuid().ToString().Substring(0, 4);
var filePath = Path.Combine("WebRootPath", "Imagef", createGUID + fileName);
using (var fileSteam = new FileStream(filePath, FileMode.Create))
{
svm.File.CopyTo(fileSteam);
}
Student student = new Student();
student.ImageDescription = fileName;
student.ImageName = filePath ;
studDBContext.students.Add(student);
studDBContext.SaveChanges();
}
return View();
}
Index.cshtml
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@item.ImageDescription
</td>
<td>
<img src="@Url.Content(item.ImageName)" alt="Image" style="width:50px;height:50px;" />
</td>
@foreach(模型中的var项目)
{
@item.ImageDescription
我想在索引页中显示图像需要帮助
我认为逻辑是,我认为图像需要整个路径,但我认为,我没有得到整个路径
我正在尝试,但图像未显示由于您的imagef文件夹不在wwwroot下,您需要设置文件路径 您可以在
启动中添加以下代码
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "WebRootPath")),
RequestPath = "/WebRootPath"
});
在你看来:
<img src="~/@Url.Content(item.ImageName)" alt="Image" style="width:50px;height:50px;" />
测试:
由于imagef文件夹不在wwwroot下,因此需要设置文件路径
您可以在启动中添加以下代码
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "WebRootPath")),
RequestPath = "/WebRootPath"
});
在你看来:
<img src="~/@Url.Content(item.ImageName)" alt="Image" style="width:50px;height:50px;" />
测试:
参见:参见: