My current action looks like this:
[HttpPost]
public void AddMessage([FromBody] ShoutboxMessage input)
{
if (!string.IsNullOrWhiteSpace(input.Message) && Request.Cookies["usrid"] != null)
{
input.SbUserId = int.Parse(Request.Cookies["usrid"]);
input.Timestamp = DateTime.UtcNow;
context.ShoutboxMessages.Add(input);
context.SaveChanges();
}
}
I would like to just do this:
[HttpPost]
public void AddMessage([FromBody] ShoutboxMessage input)
{
if (Request.Cookies["usrid"] == null)
RedirectToAction("Login");
if (!string.IsNullOrWhiteSpace(input.Message))
{
//...
}
}
but that doesn't work, obviously. Is there a way to redirect from an action that's supposed to return void? Or, the other way around, can an Action that's supposed to return an ActionResult
not result in any redirection or reload of the current page?
Edit: the "duplicate" has nothing to do with this. it may be that a void
action returns basically the same as an EmptyResult
action, but that's not the topic here, basically I want to know how to chose between an EmptyResult
and an ActionResult
at runtime.