Question

I need to a method to either pass back a string value or Redirect to another method. Is there a method class that can handle both passing back a string or redirecting to another method.

I am using MVC C#.

Was it helpful?

Solution

You can use a method with return type ActionResult

If you want to return string just use Content(string) method:

return Content("string_to_pass"); 

If you want to redirect to another action, use RedirectToAction(string) method:

return RedirectToAction("action_name");

Both Content(string) and RedirectToAction(string) are methods of MVC's Controller class.

As a summary you write your Controller like this:

public ActionResult FooDetail()
{
  if(heads_or_tails)
  {
     return Content("string_to_pass");
  }
  else
  {
     return RedirectToAction("action_name");
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top