Home > Mobile >  Trigger an action method from View
Trigger an action method from View

Time:08-13

I have an action method like this

  [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult ExternalLogin()
        {
            string provider = "";
            string returnUrl = null;
            if (returnUrl.IsNullOrWhiteSpace())
            {
                returnUrl = Request.RawUrl;
            }

            // Request a redirect to the external login provider
            return new ChallengeResult(provider,
                Url.SurfaceAction<UmbracoIdentityAccountController>("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
        }

I want to trigger this method when a particular view is rendered. I tried some thing like this

  @Html.Action("ExternalLogin", "UmbracoIdentityAccount");

But this throws an error

A public action method 'ExternalLogin' was not found on controller 

Can anyone please point out what is the right approach to do this?

CodePudding user response:

The ASP.NET MVC AntiForgeryToken works through HTTP PUT, by design.

The @Html.Action() uses [HttpGet] method not the [HttpPost] as the ExternalLogin is defined.

Therefore, the simpler way is to change the action method definition to:

[AllowAnonymous]
public ActionResult ExternalLogin()
{
    ...
}
  • Related