500 (Internal Server Error) when setting action as [ChildActionOnly]

0

I want to block partialview url via url using [ChildActionOnly] .

Issue problem, follow code:

HTML:

<li style="cursor:pointer"><a id="button_id">Criar</a></li>

JS:

$("#button_id").click(function () {
    $("#conteudoModal").load("@Url.Action("MinhaAcao", "Controller")", function () {
        $('#minhaModal').modal('show');
    });
});

Controller:

[HttpGet]
[ChildActionOnly] // <----- Aqui
public ActionResult MinhaAcao()
{
    var model = new Model();


    return PartialView(model);
}

Without [ChildActionOnly] modal opens normal and with partialview url.

With [ChildActionOnly] , when I click the "Create" button, I get error message:

  

Exception Details: System.InvalidOperationException: The action   'MyAction' is only accessible for a daughter request.

Any solution?

    
asked by anonymous 19.06.2017 / 15:52

1 answer

2

actions marked with the [ChildActionOnly] can only be called as a" child "method from within a view , using extension methods Html.Action() " or Html.RenderAction() / a>.

The load method probably makes a GET pro server request via XMLHttpRequest. That is, you are trying to call the action exactly the way you are trying to block using this attribute.

The only way out is to remove this attribute.

If you want to block the user from viewing the partial view content of the browser, you can make a request on server-side using Request.IsAjaxRequest() .

Something like:

[HttpGet]
public ActionResult MinhaAcao()
{
    if(!Request.IsAjaxRequest())
        return HttpNotFound();   

    return PartialView();
}
    
19.06.2017 / 17:36