【问题标题】:ASP.NET MVC Web Application - Facing Null Pointer ExceptionASP.NET MVC Web 应用程序 - 面临空指针异常
【发布时间】:2021-08-28 20:11:25
【问题描述】:

每当我尝试在 Employee 数据图像上调用此重定向链接时,我都会收到此异常。非常感谢任何可以解决此问题的帮助。

我的工作:

Employee查看empRecords

@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
@{
    HttpCookie cookie = Request.Cookies["Detalis"];
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EmpRecords</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <div style="float:left">
        @Html.ActionLink("Create New", "Create")
    </div>
    <div style="float:right">
        @{
            String img64 = (string)Session["userImage"];
            String img64Url = string.Format("data:image/" +
           (string)Session["userImageType"] + ";base64,{0}", img64);
            //imagetype can be e.g. gif, jpeg, png etc.
        }
        <img alt="" src="@img64Url" width="45" height="80"
             class="rounded-circle" />
        <br />
        @Html.ActionLink("Click to Logout", "Logout", "User")
    </div>
    <br />
    <p>

        @Html.Partial("_EmployeeInfo", Model)
    </p>
    <br />
    <div style="float:left">
        <dl>
            <dt>
                @Html.ActionLink("User Last Login:", cookie["Lastlogin"])
            </dt>
            <dd>
                @if (cookie != null)
                {
                    @cookie["Lastlogin"];
                }
            </dd>
        </dl>
    </div>
    <br />
</body>
</html>

部分布局视图_EmployeeInfo:

@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.First().EmployeeID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ImageURL)
        </th>
        <th>
            Image
        </th>
    </tr>
    @if (Model.Count() == 0)
    {
        <tr>
            <td colspan="6">
                No records match search criteria
            </td>
        </tr>
    }
    else
    {
        foreach (var item in Model)
        {
            using (Html.BeginForm("Delete", "Employee", new
            {
                id =
           item.EmployeeID
            }))
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.EmployeeID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem =>
                       item.FirstName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ImageURL)
                    </td>
                    <td>
                        @if (!string.IsNullOrEmpty(item.ImageURL))
                         {
                           <a href="/Employee/Details">
                              @*@Url.Content convert the relative path into application absolute path*@
                              <img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
                           </a>
                         }
                         else
                         {
                            <span>No Image Found!</span>
                         }
                    </td>
                </tr>
            }
        }
    }
</table>
@Html.PagedListPager(Model, page => Url.Action("empRecords", new { page,
SearchBy = Request.QueryString["SearchBy"], SearchTerm =
Request.QueryString["SearchTerm"], sortBy =
Request.QueryString["sortBy"] }), new PagedListRenderOptions() {
Display = PagedListDisplayMode.IfNeeded,
DisplayPageCountAndCurrentLocation = true })

所以,主要问题是每当我尝试调用此重定向链接时,都会遇到此错误:

代码:

@if (!string.IsNullOrEmpty(item.ImageURL))
{
    <a href="/Employee/Details">
        @*@Url.Content convert the relative path into application absolute path*@
        <img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
    </a>
}

错误信息:

Employee 用于以下任务的控制器函数:

public ActionResult empRecords(string SearchBy, string SearchTerm, int? page, string sortBy)
{
    //In case of Invalid user redirect to login
    if (Session["login"] == null)
    {
        return RedirectToAction("Login", "User");
    }
    EmployeeEntity entity = new EmployeeEntity();
    List<Employee> list = entity.GetList(SearchBy,SearchTerm, sortBy);
    HttpCookie cookie = Request.Cookies["Detalis"];
    if (cookie != null)
    {
        string color = cookie["Lastlogin"];
    }

    return View(list.ToPagedList(page ?? 1, 5));
}

public ActionResult Details(long id)
{
    //In case of Invalid user redirect to login
    if (Session["login"] == null)
    {
        return RedirectToAction("Login", "User");
    }
    EmployeeEntity entity = new EmployeeEntity();
    Employee employee = entity.GetSingleEmployee(id);
    HttpCookie cookie = Request.Cookies["Detalis"];
    if (cookie != null)
    {
        string color = cookie["Lastlogin"];
    }
    return View(employee);

}

Employee查看Details

@model My_Work.Models.Employee
@{
    Layout = null;
    HttpCookie cookie = Request.Cookies["Detalis"];
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Detail</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <div>
        <h4>Employee Detail</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.EmployeeID)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.EmployeeID)
            </dd>
            <dt>
                @Html.DisplayNameFor(model => model.FirstName)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.FirstName)
            </dd>

            <dt>
                @Html.DisplayNameFor(model => model.LastName)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.LastName)
            </dd>
            <dd>
                @{
                    String img64 = (string)Session["userImage"];
                    String img64Url = string.Format("data:image/" +
                   (string)Session["userImageType"] + ";base64,{0}", img64);
                    //imagetype can be e.g. gif, jpeg, png etc.
                }
                <img alt="" src="@img64Url" width="45" height="80"
                     class="rounded-circle" />
            </dd>
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeID
       }) |
        @Html.ActionLink("Back to List", "empRecords")
    </p>
</body>
</html>

【问题讨论】:

  • 重定向到/Employee/Details 时缺少将 id 作为参数传递。你的href URL应该是/Employee/Details/{id}

标签: c# asp.net asp.net-mvc image asp.net-mvc-5


【解决方案1】:

问题

您会收到上述错误,因为idEmployee 控制器中Details 函数(操作)中的强制参数。

<a href="/Employee/Details">
    @*@Url.Content convert the relative path into application absolute path*@
    <img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>

解决方案

您必须确保id 必须包含在重定向到Employee/Details 的URL 中。

网址应如下:

/Employee/Details/{id}

解决方案 1:

<a href="/Employee/Details/@item.EmployeeID">
    @*@Url.Content convert the relative path into application absolute path*@
    <img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>

解决方案 2:

<a href="@Url.Action("Details", "Employee", new { id = item.EmployeeID})">
    @*@Url.Content convert the relative path into application absolute path*@
    <img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>

【讨论】:

    【解决方案2】:

    补充说明,你也可以让id参数不强制,这样就不会给你空指针异常。在您的代码中,如果您希望 id 参数不能为空,则需要添加一些验证代码。

    public ActionResult Details(long? id = null)
    {
        // validation code example
        if (!id.HasValue)
        {
            // Redirect or return the appropriate HTTP code here
        }
    
        //In case of Invalid user redirect to login
        if (Session["login"] == null)
        {
            return RedirectToAction("Login", "User");
        }
        EmployeeEntity entity = new EmployeeEntity();
        Employee employee = entity.GetSingleEmployee(id);
        HttpCookie cookie = Request.Cookies["Detalis"];
        if (cookie != null)
        {
            string color = cookie["Lastlogin"];
        }
        return View(employee);
    
    }
    

    【讨论】:

    • 是的,这看起来是个好主意,但在我的情况下,id 参数是强制性的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多