Child actions are not allowed to perform redirect actions, after setting the site on HTTPS
This means that:
- the action corresponding to the URL has been processed and a view result has been returned.
- ASP.NET MVC's view engine has started rendering the view, possibly sending headers and part of the HTML to the browser.
- the view engine has come across a RenderAction(...) call and has started executing the action.
- the action has tried to return a Redirect HTTP status code to the browser
At this point, ASP.NET is at a loss: it has already started sending headers and HTML to the client, but now it is suddenly told to recall all that and instead send a redirect status code. It obviously cannot do that, so it throws an exception.
You will have to figure out why a redirect is being triggered and make sure that that does not happen. The most likely cause (but by no means the only possible cause) is some custom action filter that hasn't been designed to take child actions into account. ASP.NET MVCs action filters typically check whether the executing action has been called as a child action and avoid redirects in such scenarios.
Child actions are not allowed to perform redirect actions
This is not allowed because MVC has already started Rendering the View to the browser (client).
So the MVC Frameworks blocks this, because the client already receives data (html). As long as the rendering is in progress you not able to redirect in your child view.
You can return RedirectToAction
instead.
How to fix Child actions are not allowed to perform redirect actions, other answers does not fix
Instead of Calling public ActionResult CheckoutCompleteOK()
on post, remove that action and Create a HTTP Post Action for public ActionResult PaymentByBankTransfer()
.
Then return RedirectToAction("Complete");
in PaymentByBankTransfer post method.
I think this would solve your problem.
Error executing child request for handler in view
Ok I found the problem, hopefully this will help someone in future.
The controllers for the partial views each contained the [HttpGet]
attribute. For example
[HttpGet]
public ActionResult Index()
{
}
I remove the attribute from both controllers
public ActionResult Index()
{
}
and everything is now working.
Filter on Controller to check User Agent and then redirect based on if result is true
This is because you're using an ActionFilterAttribute
. If you check the documentation here: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1 it explains the filter lifecycle and basically - by the time you arrive to action filters, it's too late. You need an authorization filter or a resource filter so you can short-circuit the request.
Each filter type is executed at a different stage in the filter
pipeline:Authorization Filters
- Authorization filters run first and are used to determine whether the user is authorized for the request.
- Authorization filters short-circuit the pipeline if the request is not authorized.
Resource filters
- Run after authorization.
- OnResourceExecuting runs code before the rest of the filter pipeline. For example, OnResourceExecuting runs code before model binding.
- OnResourceExecuted runs code after the rest of
the pipeline has completed.
The example below is taken from the documentation, it's an implementation of a Resource Filter. Presumably, a similar implementation is possible with an Authorization Filter but I believe returning a valid Http Status Code after failing an Authorization Filter may be a bit of an anti-pattern.
// See that it's implementing IResourceFilter
public class ShortCircuitingResourceFilterAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
context.Result = new ContentResult()
{
Content = "Resource unavailable - header not set."
};
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
I've attempted to merge it with what you've provided - beware that this may not work out of the box.
public class ShortCircuitingResourceFilterAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
try
{
// You had duplicates in your list, try to use Hashset for .Contains methods
var crawlerSet = new Hashset<string>()
{
"facebookexternalhit/1.1",
"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
"Facebot"
};
string userAgent = HttpContext.Current.Request.UserAgent;
// You're unnecessarily and incorrectly checking if the userAgent is null multiple times
// if it's null it'll fail when you're .ToLower()'ing it.
if (!string.IsNullOrEmpty(userAgent) && crawlerSet.Contains(userAgent.ToLower()))
{
// Some crawler
context.Result = new RedirectResult("~/Home/NoRobotsAuthentication");
}
}
catch (Exception errException)
{
LogHelper.LogException(Severity.Error, errException);
SessionHelper.PolicyBase = null;
SessionHelper.ClearQuoteSession();
context.Result = new RedirectResult("~/Home/NoRobotsAuthentication");
}
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
Related Topics
How to Use the 7Z Sdk to Compress and Decompress a File
Newtonsoft JSON Deserialize Dictionary as Key/Value List from Datacontractjsonserializer
Why Does the Async Keyword Exist
Gridview Bound with Properties of Nested Class
Understanding C# Field Initialization Requirements
Use Linq to Generate Direct Update Without Select
Unable to Load Cvextern in a C# Project
Streamwriter.Write Doesn't Write to File; No Exception Thrown
Escape Quote in C# for JavaScript Consumption
How to Check How Many Messages Are in a Msmq Queue
Non-Blocking Read from Standard I/O in C#
Why Is Modulus Operator Not Working for Double in C#
How to Ignore Get-Only Properties in JSON.Net Without Using JSONignore Attributes
Set Environment Variables for a Process