如何在同一控制器中调用方法并让调用函数在 return 视图上停止执行?
How do I call a method in the same controller and have the calling function stop execution on a return View?
我有这个,
public ActionResult IndexByName(string lastName)
{
//find the name
return View("Index", myObject);
}
public ActionResult SomeOtherAction(IFormCollection collection)
{
if (collection["somekey"] == "search")
{
this.IndexByName(collection["lastname"]);
}
//other stuff in this method that I don't want to run if it says search
}
我怎样才能阻止方法 SomeOtherAction 的其余部分继续进行?我希望应用程序在另一种方法中 return 到 View("Index")。
在 C# 中,return
关键字将存在于当前方法中,return 控制它的调用方法。来自 documentation:
The return
statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void
type, the return
statement can be omitted.
例如,您可以使用这个:
public ActionResult IndexByName(string lastName)
{
return View("Index", myObject);
}
public ActionResult SomeOtherAction(IFormCollection collection)
{
if (collection["somekey"] == "search")
{
// This will return the result of IndexByName()
// and exist the SomeOtherAction method
return IndexByName(collection["lastname"]);
}
else
{
// This will return the View SomeOtherView
// and exist the SomeOtherAction method
return View("SomeOtherView");
}
// In theorty this would return an HTTP 200
// but it is NEVER hit. All execution paths
// within this method resolved before we
// ever got here.
return Ok();
}
我有这个,
public ActionResult IndexByName(string lastName)
{
//find the name
return View("Index", myObject);
}
public ActionResult SomeOtherAction(IFormCollection collection)
{
if (collection["somekey"] == "search")
{
this.IndexByName(collection["lastname"]);
}
//other stuff in this method that I don't want to run if it says search
}
我怎样才能阻止方法 SomeOtherAction 的其余部分继续进行?我希望应用程序在另一种方法中 return 到 View("Index")。
在 C# 中,return
关键字将存在于当前方法中,return 控制它的调用方法。来自 documentation:
The
return
statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is avoid
type, thereturn
statement can be omitted.
例如,您可以使用这个:
public ActionResult IndexByName(string lastName)
{
return View("Index", myObject);
}
public ActionResult SomeOtherAction(IFormCollection collection)
{
if (collection["somekey"] == "search")
{
// This will return the result of IndexByName()
// and exist the SomeOtherAction method
return IndexByName(collection["lastname"]);
}
else
{
// This will return the View SomeOtherView
// and exist the SomeOtherAction method
return View("SomeOtherView");
}
// In theorty this would return an HTTP 200
// but it is NEVER hit. All execution paths
// within this method resolved before we
// ever got here.
return Ok();
}