Model view controller 如何在MVCContrib中显示来自fluent controller的无效调用异常?

Model view controller 如何在MVCContrib中显示来自fluent controller的无效调用异常?,model-view-controller,exception-handling,controller,mvccontrib,Model View Controller,Exception Handling,Controller,Mvccontrib,如何通过MVCContrib.FluentController CheckValidCall(操作)中的操作传递异常thorwn 当GetResults()抛出异常时,我希望在视图中显示它。我累了 <%if (ViewData.ModelState.ContainsKey("_FORM")) {%> <div class="notificationError"> <%= ViewData.ModelState["_FORM"].Err

如何通过MVCContrib.FluentController CheckValidCall(操作)中的操作传递异常thorwn

当GetResults()抛出异常时,我希望在视图中显示它。我累了

    <%if (ViewData.ModelState.ContainsKey("_FORM")) {%>
    <div class="notificationError">
        <%= ViewData.ModelState["_FORM"].Errors.FirstOrDefault().ErrorMessage %>            
    </div>
<%}%>

但是ModelState是有效的,并且不包含错误。有没有办法访问异常消息而不在try-catch块中包装服务方法?如果有帮助的话,这里是我的单元测试,以检查失败的ModelState,因为TestController.ModelState.IsValid为true:

    [Fact]
    public void ServiceExceptionIsConvertedToModelStateErrorInFluentController()
    {
        // Set up
        MockService.Setup(x => x.GetResults(It.IsAny<int>(), It.IsAny<int>()))
            .Throws(new InvalidOperationException("Mocked Service Exception"));

        // Excercise
        Assert.Throws<InvalidOperationException>(() => TestController.GetResults(1, 1));

        // Verify
        Assert.False(TestController.ModelState.IsValid);
        Assert.True(TestController.ModelState["_FORM"].Errors.Count > 0);
    }
[事实]
public void Service ExceptionsConvertedToModelStateErrorInfluenceController()
{
//设立
MockService.Setup(x=>x.GetResults(It.IsAny(),It.IsAny())
.Throws(新的InvalidOperationException(“模拟服务异常”));
//锻炼
抛出(()=>TestController.GetResults(1,1));
//核实
False(TestController.ModelState.IsValid);
Assert.True(TestController.ModelState[“_FORM”].Errors.Count>0);
}

我已通过重写MvcContrib.FluentController.AbstractFluentController.ExecuteCkValidCall(函数操作)将异常传递到ModelState:

protectedoverride对象ExecuteCheckValidCall(Func操作)
{
尝试
{
返回base.ExecuteCheckValidCall(操作);
}
捕获(异常)
{
AddModelError(“异常”,异常);
返回null;
}
}
由CheckValidCall调用。但是,该方法被描述为“仅用于测试目的,不应使用”,替代方法是重写MvcContrib.FluentController.AbstractFluentController.CheckValidCall()

    [Fact]
    public void ServiceExceptionIsConvertedToModelStateErrorInFluentController()
    {
        // Set up
        MockService.Setup(x => x.GetResults(It.IsAny<int>(), It.IsAny<int>()))
            .Throws(new InvalidOperationException("Mocked Service Exception"));

        // Excercise
        Assert.Throws<InvalidOperationException>(() => TestController.GetResults(1, 1));

        // Verify
        Assert.False(TestController.ModelState.IsValid);
        Assert.True(TestController.ModelState["_FORM"].Errors.Count > 0);
    }
    protected override object ExecuteCheckValidCall(Func<object> action)
    {
        try
        {
            return base.ExecuteCheckValidCall(action);
        }
        catch (Exception exception)
        {
            ModelState.AddModelError("_Exception", exception);
            return null;
        }
    }