简体   繁体   English

从ASP.NET MVC框架迁移到ASP.NET Core 3.1 MVC后图像不显示

[英]Image does not display after migration from ASP.NET MVC framework to ASP.NET Core 3.1 MVC

After migrating my ASP.NET MVC project to ASP.NET Core 3.1 MVC, the images that are in the project folders are no longer appearing in the view.将我的 ASP.NET MVC 项目迁移到 ASP.NET Core 3.1 MVC 后,项目文件夹中的图像不再出现在视图中。

Please, what is happening?请问,这是怎么回事?

<img src="@ViewBag.ImagePath" class="rounded img-fluid" alt="" id="Image1">

When using .net core you need to explicitly configure the application to serve static files like.css, .js or image files.使用 .net 核心时,您需要明确配置应用程序以提供 static 文件,如 .css、.js 或图像文件。

To configure this function go to your Startup.cs file, then inside the method Configure(IApplicationBuilder app, IWebHostEnvironment env) add the following line: app.UseStaticFiles();要将此 function go 配置到您的Startup.cs文件,然后在方法Configure(IApplicationBuilder app, IWebHostEnvironment env)中添加以下行: app.UseStaticFiles(); before app.UseRouting();app.UseRouting();

If your application is already configured to serve static files, please, leave a comment below so we can work on it.如果您的应用程序已配置为提供 static 个文件,请在下方发表评论,以便我们进行处理。


Note that in .NET Core image files need to be located inside wwwroot > images and refered like this <img src="~/images/Image1.jpg" class="rounded img-fluid" alt="" id="Image1">请注意,在 .NET 中,核心图像文件需要位于wwwroot > images中,并像这样<img src="~/images/Image1.jpg" class="rounded img-fluid" alt="" id="Image1">

In .net core the folder structure is different.在.net核心文件夹结构不同。 Images need to be stored in wwwroot folder.图像需要存储在 wwwroot 文件夹中。 Also you need to enable static files middleware in您还需要启用 static 文件中间件

Startup.cs启动.cs

file in归档

Configure配置

method before using MVC or routing.使用 MVC 或路由之前的方法。

app.UseStaticFiles(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core MVC 中显示数据库中的图像? - How to display an image from the database in ASP.NET Core MVC? Asp.net MVC核心TagHelpers迁移后突然无法使用 - Asp.net mvc core TagHelpers not working Suddenly after migration 从 ASP.NET MVC 迁移到 ASP.NET 核心 MVC - 操作 model 绑定导致 Z37A6259CC6491DAE299A8D - Migration from ASP.NET MVC to ASP.NET Core MVC - action model binding resulting null 在 ASP.NET Core 3.1 中显示来自数据库的图像 - Display Image from database in ASP.NET Core 3.1 asp.net mvc(迁移) - asp.net mvc (Migration) 授权成功后,在 ASP.NET Core 3.1 MVC 中从 AWS Cognito 检索 JWT 令牌 - Retrieving JWT token from AWS Cognito in ASP.NET Core 3.1 MVC after successful authorization 从ASP.NET Core 1.1 MVC迁移到2.0后,自定义cookie身份验证无法正常工作 - Custom cookie authentication not working after migration from ASP.NET Core 1.1 MVC to 2.0 如何在 ASP.NET Core 3.1 MVC 中进行自定义路由 - How to do custom routing in ASP.NET Core 3.1 MVC 带有 Blazor 的 ASP.NET Core 3.1 MVC - 缺少程序集 - ASP.NET Core 3.1 MVC with Blazor - Missing Assembly 将 ASP.NET CORE 3.1 MVC 应用程序部署到 azure 时出现问题 - Problem deploying ASP.NET CORE 3.1 MVC app to azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM