简体   繁体   English

加载资源失败:服务器响应状态为 404 ()...在我右键单击并单击查看页面上的“INSPECT”后,我收到此错误

[英]Failed to load resource: the server responded with a status of 404 ()…After I right click and click on “INSPECT” on my view page I get this error

I am a beginner at MVC and trying to create a page.我是 MVC 的初学者并试图创建一个页面。 I have three tables in my database.我的数据库中有三个表。 I am using Entity Framework with a database-first approach and I created these three tables as classes in my Models folder.我正在使用具有数据库优先方法的实体框架,并将这三个表创建为我的Models文件夹中的类。 I also created a new class called AllClasses and defined each of this three classes in this new class.我还创建了一个名为AllClasses的新 class,并在这个新的 class 中定义了这三个类中的每一个。

I am trying to get 3 pictures from the tblCharacter tables' CharacterImageURL column.我正在尝试从tblCharacter表的CharacterImageURL列中获取 3 张图片。

When I run my code, the program is running without any error.当我运行我的代码时,程序运行没有任何错误。 So what is the problem?那么问题是什么?

The problem is I can not display these three photo on my view page.问题是我无法在我的视图页面上显示这三张照片。 I will add some screenshots to show you problem more detailed.我将添加一些屏幕截图以更详细地向您展示问题。 BY THE WAY I HAVE IMAGE URL'S ON THE DATABASE NOT THE IMAGES LIKE.jpg.顺便说一句,我在数据库中有图像 URL,而不是 LIKE.jpg 图像。 What I have on my database is spmething like images/avatar/Chewbacca-icon.png I KNOW THERE ARE SIMILAR QUESTIONS BUT NONE OF THE SOLUTIONS WORKED FOR ME...我的数据库中的内容类似于images/avatar/Chewbacca-icon.png我知道有类似的问题,但没有一个解决方案适合我......

This is my controller:这是我的 controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StarWars.Models;

namespace StarWars.Controllers
{
    public class HomeController : Controller
    {
        StarWarsFunClubDBEntities myobject = new StarWarsFunClubDBEntities();

        public ActionResult Index()
        {
            var myclass = new All();
            myclass.Characters = myobject.tblCharacters.ToList();

            return View(myclass);
        }

        public ActionResult SWHome()
        {
            var myclass = new AllClasses();
            myclass.Avatars = myobject.tblAvatars.ToList();
            myclass.Characters = myobject.tblCharacters.ToList();
            myclass.Comments = myobject.tblComments.ToList();
            return View(myclass);
        }
    }
}

This is my home page SWHome.cshtml :这是我的主页SWHome.cshtml

@model StarWars.Models.AllClasses

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>SWHome</title>
    <link rel="stylesheet" type="text/css" href="~/Content/css.css" />
</head>
<body>
    <div class="box">
        <div class="header">
            <div class="logo"> <img src="~/images/Star_Wars_Logo.png" alt="Alternate Text" /> </div>
            <div class="sitename"><h4>Star Wars Fun Club</h4></div>
        </div>
        <div class="question"><h5>What do you think about the new characters of Star Wars: The Force Awakens(2015)?</h5></div>
        <div class="photo">
            @foreach (var item in Model.Characters)
            {   
                <div class="innerphoto">
                    <img src="@item.CharacterImageURL" alt="" />
                </div>
            }

        </div>
        <div class="comment"></div>
        <div class="addcomment"></div>
        <div class="footer"></div>
    </div>
</body>
</html>

These are my auto-generated classes:这些是我自动生成的类:

namespace StarWars.Models
{
    using System;
    using System.Collections.Generic;

    public partial class tblAvatar
    {
        public int AvatarID { get; set; }
        public string AvatarName { get; set; }
        public string AvatarImageURL { get; set; }
    }

    public partial class tblComment
    {
        public int CommentID { get; set; }
        public string Expression { get; set; }
        public System.DateTime DateTime { get; set; }
        public int AvatarID { get; set; }
    }

    public partial class tblCharacter
    {
        public int CharacterID { get; set; }
        public string CharacterName { get; set; }
        public string CharacterImageURL { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StarWars.Models;
using StarWars.Controllers;
using System.Data.Entity;

namespace StarWars.Models
{
    public class AllClasses
    {
        public IEnumerable<tblAvatar> Avatars { get; set; }
        public IEnumerable<tblCharacter> Characters { get; set; }
        public IEnumerable<tblComment> Comments { get; set; }
    }
}

This is my view ( SWHome ) where I want to show the photos:这是我想展示照片的视图( SWHome ):

在此处输入图像描述

This screenshot shows the problem at the bottom:此屏幕截图显示了底部的问题:

如您所见,在 HTML <div class="innerphoto"> 部分图像源中可以看到,但我无法在我的视图页面上获取它们......所以我认为问题不在于数据库连接......

As you see, in the HTML <div class="innerphoto"> , image sources can be seen but I can't get them to be displayed on my view page.如您所见,在 HTML <div class="innerphoto">中,可以看到图像源,但我无法将它们显示在我的视图页面上。 So I think the problem is not with the DB connection.所以我认为问题不在于数据库连接。

This is my solution explorer:这是我的解决方案资源管理器:

在此处输入图像描述

I have new screeshoot for the one who interested in:我为感兴趣的人提供了新的截图: 红色的是我无法在我的视图页面上显示的照片......也许这将有助于解决问题的人之一。

Problem is here:问题在这里:

<img src="@item.CharacterImageURL" alt="" />

You cannot access the image from root directory (ie wwwroot) directly.您不能直接从根目录(即 wwwroot)访问图像。

Try this:尝试这个:

<img src= "@Url.Content(Model.YourImageLocationPropertyHere)" />

Url.Content is used when you wish to resolve a URL for any file or resource on your site and you would pass it the relative path. Url.Content 当您希望为站点上的任何文件或资源解析 URL 并且您将传递相对路径时使用。

Hope this helps.希望这可以帮助。

暂无
暂无

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

相关问题 加载资源失败:服务器响应状态为 404() - Failed to load resource: the server responded with a status of 404 () net core 2无法加载资源:服务器响应状态为404(未找到) - net core 2 Failed to load resource: the server responded with a status of 404 (Not Found) ASP无法加载资源:服务器在服务器中响应状态为404(未找到)错误 - ASP Failed to load resource: the server responded with a status of 404 (Not Found) error in server 从 angular 调用 .net 核心方法给出错误:加载资源失败:服务器响应状态为 404 () - calling .net core method from angular gives error: failed to load resource: the server responded with a status of 404 () 加载资源失败:服务器响应状态为500(内部服务器错误 - Failed to load resource: the server responded with a status of 500 (Internal Server Error 错误:无法加载资源:服务器的状态为400(错误请求) - Error:Failed to load resource: the server responded with a status of 400 (Bad Request) ASP.NET CORE 2.2 新项目,无法加载资源:服务器在 JS/CSS/Image 的服务器中响应状态为 404(未找到)错误 - ASP.NET CORE 2.2 New Project, Failed to load resource: the server responded with a status of 404 (Not Found) error in server for JS/CSS/Image 无法加载资源:服务器在Asp.NET GridView中以404(未找到)状态响应 - Failed to load resource: the server responded with a status of 404 (Not Found) in Asp.NET GridView 加载资源失败:服务器响应状态为 403 () on server - Failed to load resource: the server responded with a status of 403 () on server 加载资源失败:服务器响应状态为500(内部服务器错误)asp.net - Failed to load resource: the server responded with a status of 500 (Internal Server Error) asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM