C# 图像未显示在索引页中
我的索引页有问题。在这个索引页面上,我们可以看到上传的广告详细信息,如广告标题、说明和链接,除了上传的图像。当我检查时,我能看见C# 图像未显示在索引页中,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,我的索引页有问题。在这个索引页面上,我们可以看到上传的广告详细信息,如广告标题、说明和链接,除了上传的图像。当我检查时,我能看见 <img src="/images/Ad/ad2.png" width="50px" height="50px"> 在索引页面上,我使用以下代码 <td> <img src="@Url.Content(String.Format("/images
<img src="/images/Ad/ad2.png" width="50px" height="50px">
在索引页面上,我使用以下代码
<td>
<img src="@Url.Content(String.Format("/images/Ad/{0}",item.ImagePath))" width="50px" height="50px" />
</td>
这是我的控制器
public IActionResult Create([Bind("AdID,Title,ImagePath,Description,Link")] AdTbl adTbl, List<IFormFile> FormFile)
{
if (FormFile != null && FormFile.Count > 0)
{
string folderName = "images/Ad";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
foreach (IFormFile item in FormFile)
{
if (item.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(newPath, fileName);
adTbl.AdID = Guid.NewGuid();
adTbl.ImagePath = fileName;
using (var stream = new FileStream(fullPath, FileMode.Create))
{
item.CopyTo(stream);
}
_adTblRepository.CreateAd(adTbl);
}
}
_notyf.Success("Advertisement added successfully");
return RedirectToAction(nameof(Index));
}
return View(adTbl);
}
public IActionResult Create([Bind(“AdID,Title,ImagePath,Description,Link”)]AdTbl AdTbl,List FormFile)
{
if(FormFile!=null&&FormFile.Count>0)
{
字符串folderName=“images/Ad”;
字符串webRootPath=\u hostingEnvironment.webRootPath;
字符串newPath=Path.Combine(webRootPath,folderName);
如果(!Directory.Exists(newPath))
{
CreateDirectory(newPath);
}
foreach(FormFile中的FormFile项)
{
如果(item.Length>0)
{
string fileName=ContentDispositionHeaderValue.Parse(item.ContentDisposition.fileName.Trim(“”);
字符串fullPath=Path.Combine(新路径,文件名);
adTbl.AdID=Guid.NewGuid();
adTbl.ImagePath=文件名;
使用(var stream=newfilestream(fullPath,FileMode.Create))
{
项目。复制到(流);
}
_adTblRepository.CreateAd(adTbl);
}
}
_notyf.成功(“成功添加广告”);
返回重定向到操作(名称(索引));
}
返回视图(adTbl);
}
**最重要的是,相同的代码在不同的页面上使用,这是有效的。我注意到,当我们更改位置时,这个问题再次出现您是否使用
base=“~”
在\u Layout.cshtml
文件中定义了基根?您最好提供您的图像文件结构(在wwwroot中?),索引视图及其相关操作,我测试了您的代码,但效果很好。