Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC5中从帮助器类返回视图_C#_Asp.net Mvc - Fatal编程技术网

C# 在MVC5中从帮助器类返回视图

C# 在MVC5中从帮助器类返回视图,c#,asp.net-mvc,C#,Asp.net Mvc,我在助手类中有一个try-catch段。如果try-catch段抛出错误,我想返回一个视图。现在我有: try { //some code } catch (Exception ex) { return View("~/Views/Home/Index.cshtml"); } 试试这个: try { } catch (System.Exception) { return RedirectToAction("Index", "Home"); } 试试这个: try {

我在助手类中有一个try-catch段。如果try-catch段抛出错误,我想返回一个视图。现在我有:

try
{
    //some code
}
catch (Exception ex)
{
    return View("~/Views/Home/Index.cshtml");
}
试试这个:

try
{

}
catch (System.Exception)
{
    return RedirectToAction("Index", "Home");
}
试试这个:

try
{

}
catch (System.Exception)
{
    return RedirectToAction("Index", "Home");
}
有两种方法:
1.(重新)在帮助程序中抛出异常并向控制器添加异常处理,或
2.在帮助程序中捕获异常,例如返回bool以表示成功/失败并在控制器中响应此值

第一种方法:

MyController.cs

public ActionResult Index()
{
    try
    {
        MyHelper.SomeMethod(); // This method is allowed to throw
        return View(); // No view name specified means this renders "Index"
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}
public ActionResult Index()
{
    bool succeeded = MyHelper.SomeFunction(); // This method should never throw
    if (succeeded)
        return View(); // No view name specified means this renders "Index"
    else
        return View("Error");
}
第二种方法:

MyController.cs

public ActionResult Index()
{
    try
    {
        MyHelper.SomeMethod(); // This method is allowed to throw
        return View(); // No view name specified means this renders "Index"
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}
public ActionResult Index()
{
    bool succeeded = MyHelper.SomeFunction(); // This method should never throw
    if (succeeded)
        return View(); // No view name specified means this renders "Index"
    else
        return View("Error");
}
此外,尽量避免在视图中调用帮助程序,除非您的视图可以处理其所有结果。当一个视图已经在执行时,您不能简单地将其“切换”到另一个视图,这将是控制器的任务和责任。

有两种方法:
1.(重新)在帮助程序中抛出异常并向控制器添加异常处理,或
2.在帮助程序中捕获异常,例如返回bool以表示成功/失败并在控制器中响应此值

第一种方法:

MyController.cs

public ActionResult Index()
{
    try
    {
        MyHelper.SomeMethod(); // This method is allowed to throw
        return View(); // No view name specified means this renders "Index"
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}
public ActionResult Index()
{
    bool succeeded = MyHelper.SomeFunction(); // This method should never throw
    if (succeeded)
        return View(); // No view name specified means this renders "Index"
    else
        return View("Error");
}
第二种方法:

MyController.cs

public ActionResult Index()
{
    try
    {
        MyHelper.SomeMethod(); // This method is allowed to throw
        return View(); // No view name specified means this renders "Index"
    }
    catch (Exception ex)
    {
        return View("Error");
    }
}
public ActionResult Index()
{
    bool succeeded = MyHelper.SomeFunction(); // This method should never throw
    if (succeeded)
        return View(); // No view name specified means this renders "Index"
    else
        return View("Error");
}


此外,尽量避免在视图中调用帮助程序,除非您的视图可以处理其所有结果。当一个视图已经在执行时,你不能简单地将它“切换”到另一个视图,这将是控制器的任务和责任。

你到底在问什么?我想返回视图~/Views/Home/Index.cshtml,因为我在帮助类而不是控制器中,返回视图(~/Views/Home/Index.cshtml);不工作您是否允许将异常抛出回控制器,以便它可以处理异常并执行重定向?是,但是我不知道怎么做:)你能发布调用这个helper类的控制器代码吗?你到底在问什么?我想返回视图~/Views/Home/Index.cshtml,因为我在helper类而不是控制器中,返回视图(“~/Views/Home/Index.cshtml”);不工作您是否可以将异常抛出回控制器,以便它可以处理它并执行重定向?是的,但我不知道如何执行:)您可以发布调用此帮助程序类的控制器代码吗?我收到消息“重定向到操作”在当前上下文中不存在。可能是因为我不在控制器中,而是在助手类中。您应该尝试从控制器捕获并重定向。在这种情况下,我不敢这样做。我正在使用System.Xml;和System.ServiceModel.Syndication;。当我开始一个新项目时,VS在找到system.xml的正确版本时遇到了问题,所以现在我不敢碰这段代码。可能太偏执了:)您无法从请求上下文之外重定向。如果您在请求上下文中,您有一个控制器可以管理您的错误。我得到消息重定向到操作在当前上下文中不存在。可能是因为我不在控制器中,而是在助手类中。您应该尝试从控制器捕获并重定向。在这种情况下,我不敢这样做。我正在使用System.Xml;和System.ServiceModel.Syndication;。当我开始一个新项目时,VS在找到system.xml的正确版本时遇到了问题,所以现在我不敢碰这段代码。可能太偏执了:)您无法从请求上下文之外重定向。如果您处于请求上下文中,则有一个控制器可以管理您的错误。问题是,当用户单击索引视图中的按钮时,可能会发生异常。我可能可以通过使用局部视图来解决这个问题,但我不希望这样。有什么方法可以从helper类调用控制器吗?(项目已经是意大利面代码,所以它不必是最佳实践)我想我找到了一种使用您的方法的方法。让我来测试一下:)效果很好:)问题是,当用户单击索引视图中的按钮时,可能会发生异常。我可能可以通过使用局部视图来解决这个问题,但我不希望这样。有什么方法可以从helper类调用控制器吗?(项目已经是意大利面代码,所以它不必是最佳实践)我想我找到了一种使用您的方法的方法。让我来测试一下:)效果很好:)