asp.net-mvc - 在MVC中显示捕获异常

标签 asp.net-mvc asp.net-mvc-4 error-handling exception-handling

我对Development and MVC很陌生。
我试图显示我的MVC站点中存储库引发的异常。我创建了View:“Error”来处理错误消息。
我的主 Controller 如下所示:

using BankMVC.Models;
using BankMVC.WithdrawingService;
using Pocos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BankMVC.Controllers
{
    public class WithdrawingController : Controller
    {
        WithdrawingServiceClient withdrawingClient;

        public WithdrawingController()
        {
            withdrawingClient = new WithdrawingServiceClient();
        }

        public ActionResult Withdraw(int Id, int TypeId)
        {
            BankAccount bankAccount = new BankAccount();
            bankAccount.BankAccountId = Id;
            bankAccount.BankAccountTypeId = TypeId;
            DepositingWithdrawingViewModel withdrawViewModel =
                new DepositingWithdrawingViewModel();
            withdrawViewModel.BankAccountId = bankAccount.BankAccountId;
            withdrawViewModel.Balance = bankAccount.Balance;
            withdrawViewModel.BankAccountTypeId = bankAccount.BankAccountTypeId;
            return View(withdrawViewModel);
        }

        [HttpPost]
        public ActionResult Withdraw(DepositingWithdrawingViewModel withdrawViewModel)
        {
            BankAccount bankAccount = new BankAccount();
            bankAccount.BankAccountId = withdrawViewModel.BankAccountId;
            bankAccount.BankAccountTypeId = withdrawViewModel.BankAccountTypeId;

            try
            {
                withdrawingClient.Withdraw(withdrawViewModel.Amount, bankAccount);
                return RedirectToAction("Index", "BankAccount");
            }

            catch (Exception e)
            {
                return RedirectToAction("Error", new { message = e.Message });
            }

        }

    }
}

我的主 Controller View 如下所示:
@model BankMVC.Models.DepositingWithdrawingViewModel

@{
    ViewBag.Title = "Withdraw";
}

<h2>Withdraw</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Withdraw from Bank Account</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.BankAccountId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BankAccountId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BankAccountId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BankAccountTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BankAccountTypeId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BankAccountTypeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Balance, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Balance, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Balance, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Withdraw" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index", "BankAccount")
</div>

@section Scripts {
        @Scripts.Render("~/bundles/jqueryval")

}

我创建了一个错误模型,看起来如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BankMVC.Models
{
    public class ErrorModel
    {
        public string Error { get; set; }
    }
}

我的错误 View 应如何显示错误消息。当前看起来如下:
@model BankMVC.Models.ErrorModel

@{
    ViewBag.Title = "Error";
}

<h2>Error</h2>

任何澄清表示赞赏。

最佳答案

我看到您正在重定向到名为“错误”的操作,以显示您的错误页面。但是我看不到的是一个以“错误”为名的 Action 方法。请执行下列操作

在名为“错误”的 Controller 中创建另一个方法

public ActionResult Error(string message)
{
    var model = new ErrorModel();
    model.Error = message;
    return View(model); // make sure your error view is named as "Error". Else u need to specify the view name in the return command.
}

请注意,我已经利用了您发布的错误模型。

同样在“错误 View ”中,您可以通过更改 View 来利用错误消息
@model BankMVC.Models.ErrorModel

@{
    ViewBag.Title = "Error";
}

<h2>Error</h2>
<h2>Message : @Model.Error</h2>

这也会显示您的错误消息

关于asp.net-mvc - 在MVC中显示捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46041125/

相关文章:

c# - Controller 中的 MVC3 自动完成和 POST 方法

asp.net-mvc-4 - MVC4 JSON.Net oob

c# - 禁用 WebAPI 的 Windows 身份验证

c# - 在Gtk#应用程序中将全局异常处理程序放在哪里?

c# - ASP.NET 身份 OWIN 中间件 Google OAuth2 AuthenticationManager 登录不工作

sql-server - 在数据库的引用表中记录代表 "all other records"的记录是个好主意吗?

asp.net-mvc - MVC Response.RedirectToRoute(RouteData.Values) 重定向到区域 Controller

if-statement - Go 是否优化了无法访问的 if 语句?

python - 在python中读取dat文件显示UnicodeDecodeError(文件<class '_io.TextIOWrapper'>)

javascript - @Html.DropDownListFor() 即使选择了列表项也始终显示空白