asp.net mvcのApp_Web_xxxx.dllで発生したSystem.NullReferenceExceptionを修正するにはどうすればよいですか?

MikeJoe

これは、ManageControllerのインデックスアクションのビューコードの一部です。

<div class="mngimg">
    @using (Html.BeginForm("UploadPhoto", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div class="btn btn-default browseimg">
            <input type="file" name="file" id="files" onchange="this.form.submit()" />
        </div>
        <div class="btn btn-default browseimg">
            @Html.ActionLink("Remove Photo", "RemovePhoto", "Manage")
        </div>
    }
</div>
                </div>
            }
        </dd>
<dt>Password:</dt>
<dd>
    [
    @if (Model.HasPassword) <!-- Here is my error. The Model is null -->
            {
        @Html.ActionLink("Change your password", "ChangePassword")
    }
    else
    {
        @Html.ActionLink("Create", "SetPassword")
    }
    ]
</dd>

このページを開いて[写真を削除]をクリックすると、次のようなエラーが表示され続けますAn exception of type 'System.NullReferenceException' occurred in App_Web_ckoryptg.dll but was not handled in user codeデバッグを試みましたが、Model.HasPasswordがnullになる理由がわかりません。これが、ManageControllerからのRemovePhotoアクションです。

[HttpPost]
public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        var user = await GetCurrentUserAsync();
        var userId = user.Id;
        var fileExt = Path.GetExtension(file.FileName);
        var fnm = userId + ".png";
        if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg") || fileExt.ToLower().EndsWith(".gif"))// Important for security if saving in webroot
        {
            var filePath = HostingEnvironment.MapPath("~/Content/Images/") + fnm;
            var directory = new DirectoryInfo(HostingEnvironment.MapPath("~/Content/Images/"));
            if (directory.Exists == false)
            {
                directory.Create();
            }
            ViewBag.FilePath = filePath.ToString();
            file.SaveAs(filePath);
            return RedirectToAction("Index", new { Message = ManageMessageId.PhotoUploadSuccess });
        }
        else
        {
            return RedirectToAction("Index", new { Message = ManageMessageId.FileExtensionError });
        }
    }
    return RedirectToAction("Index", new { Message = ManageMessageId.Error });// PRG
}

private async Task<ApplicationUser> GetCurrentUserAsync()
{
    return await UserManager.FindByIdAsync(User.Identity.GetUserId());
}

Visual Studioに付属するデフォルトのMVCプロジェクトを開き、このチュートリアルASP.NETアップロード画像から従ったこれらの追加事項を追加しましたこれを解決するにはどうすればよいですか?

編集:これは私のRemovePhotoアクションです。

public ActionResult RemovePhoto()
        {
            string file = "~/Content/Images/" + User.Identity.GetUserId() + ".png";
            if(System.IO.File.Exists(Server.MapPath(file)))
                System.IO.File.Delete(Server.MapPath(file));
            return View("Index");
        }
mxmissile

Indexアクションにリダイレクトするだけです。そうすれば、RemovePhotoアクションでインデックスモデルをインスタンス化する必要がありませんこのパターンの詳細については、こちらをご覧ください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ