2

I can't believe I cannot find any solution to this. For business reason, RedirectToAction or anything won't do because it causes a HTTP 302 to be returned. An example:

public async Task<IActionResult> GoogleLoginCallback(GoogleLoginCallbackViewModel requestModel)
{
    if (requestModel == null || !requestModel.Error.IsNullOrEmpty())
    {
        // Return something here
        // to process at Login Action instead
        // maybe with Error data passed
    }

    // Normal processing
}

The nearest I can find is this post using TransferResult but:

  1. It seems ugly to me.
  2. Does not work for ASP.NET Core.

Is there anyway to transfer the request to another Route/Action without returning a HTTP Redirection?

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • You should be able to rewrite it, https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-2.1&tabs=aspnetcore2x – Lex Li Oct 06 '18 at 18:59
  • 2
    If it's in the same controller, simply call the other method directly and return its result. If it's in another controller, see https://stackoverflow.com/q/16870413/11683. – GSerg Oct 07 '18 at 07:59
  • @GSerg yeah I think that is the best way for now. (Note: for .NET Core, I use manual Service Provider from this post: https://stackoverflow.com/questions/32459670/resolving-instances-with-asp-net-core-di) – Luke Vo Oct 07 '18 at 10:48
  • Why do you not want to redirect? That's how it *should* be done. – Chris Pratt Oct 08 '18 at 13:57
  • @ChrisPratt I wish I could tell, but it's a very specific business logic as I stated in the question. I know it's the right way (and using `TempData` to transfer data), but for this case only I am not allowed to return any redirect HTTP response. – Luke Vo Oct 08 '18 at 15:30
  • 2
    Well, *not* redirecting can have numerous side effects. Since you're essentially returning the same thing another route does at a different route, you can end up with duplicate content penalties from search engines. Additionally, if you have form which does a postback (as is typical), it will post to the URL it was server under, not where it actually needs to go, which can cause things to break dramatically. Sometimes requirements don't make sense. It's your job as a developer to call out such instances as much as it is to implement the requirements. – Chris Pratt Oct 08 '18 at 15:39
  • At the very least, sometimes things are requested only because the stakeholder doesn't actually understand what's going on. I have a hard time seeing how a requirement like this can be privileged, even in the abstract, but it's possible that there's actually a better way to meet the requirement than your proposed solution here. – Chris Pratt Oct 08 '18 at 15:42
  • I will mention it tomorrow and hope we can implement the proper way! – Luke Vo Oct 08 '18 at 15:46

0 Answers0