0

I am getting below error:

Child actions are not allowed to perform redirect actions.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Child actions are not allowed to perform redirect actions.

My Code is as below:

[RequireHttp(RequireSecure = false)]
public class ABCController : BaseController
{
    public ActionResult XYZ()
    {           
        return PartialView(ViewName);            
    }
}

View

  @{Html.RenderAction("XYZ", "ABC");}

RequireHTTP

public class RequireHttpAttribute : System.Web.Mvc.RequireHttpsAttribute
{
    public bool RequireSecure = false;
    public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        if (RequireSecure)
        { 
            // default RequireHttps functionality
            base.OnAuthorization(filterContext);
        }
        else
        {
            // non secure requested
            if (filterContext.HttpContext.Request.IsSecureConnection)
            {
                HandleNonHttpRequest(filterContext);
            }
        }
    }

    protected virtual void HandleNonHttpRequest(AuthorizationContext filterContext)
    {
        if (String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            // redirect to HTTP version of page
            string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
            filterContext.Result = new RedirectResult(url);
        }
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Amit
  • 15,217
  • 8
  • 46
  • 68
  • If your parent action is called with HTTPS, why not make the child action request on HTTPS as well? Why does it need to be over HTTP? You could just decorate all of the actions that don't return partial views with the `RequireHttp` attribute instead of the entire controller class. – Dan Sep 05 '13 at 06:51
  • @DAN I can not call all on HTTPS, this is my use case. – Amit Sep 05 '13 at 06:58
  • Like I said, don't decorate the class; decorate all of the top-level actions that don't return partial views. – Dan Sep 05 '13 at 07:02
  • Why does a view REQUIRE http rather than https? There are of course client-side templating options that could work. – Aron Sep 05 '13 at 07:05
  • @Aron can you explain ? – Amit Sep 05 '13 at 07:07
  • @DAN I have many action some required https and some not. – Amit Sep 05 '13 at 07:08
  • The problem is that you have a page requested over HTTPS that renders by making an internal request to an action that renders a partial view that you have decorated with the `RequireHttp` attribute. You can't do a redirect when requesting an action that returns a partial view. If you take the attribute off the class and only decorate top-level actions with it, this will solve your problem. – Dan Sep 05 '13 at 07:11
  • It is not clear why you would FORCE a client to GET using http rather than https. However you could use AJAX/IFRAME/XSLT etc to have the client include the child view into your parent view by getting it directly, using HTTP. – Aron Sep 05 '13 at 07:11
  • There is a reason why HTTP 1.1 has an upgrade header for forcing a client to switch to HTTPS. But they never created a header for the opposite. – Aron Sep 05 '13 at 07:13
  • Is your view just a RenderAction ? If so, why don't you just return the child action's result in the parent action ? (sorry if stupid question, far from being MVC ninja) – jbl Sep 05 '13 at 07:40

0 Answers0