500 (Internal Server Error) when defining action as [Childactiononly]

Asked

Viewed 240 times

0

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

Face 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], by clicking on "Create" button, I get error message:

Exception Details: System.Invalidoperationexception: The Action 'Minhaacao' is accessible only for a daughter request.

Some solution ?

  • What is the error message?

  • 1

    @LINQ Error 500 as far as I understand.

  • 1

    @Aline 500 That code says absolutely nothing. It is only a number that identifies the type of the HTTP response, where semantically it means there was an error on the server side. ASP.NET applications usually return a 500 response when an exception occurs, if an exception occurs there is a message and a stacktrace.

  • 1

    @LINQ, relax. If I have any route problem or permission, will return error 500 and no other information. That’s what I understand is happening to him. If you know how to provide more detailed information when it is not on the console, please reply, because I also want to know. = T

  • 1

    @Aline did not understand the "relax". About the error message: you are mistaken, of course this will not happen. Error 500 will always return a page with the information that originated the exception. If the call is made via Javascript it is possible to see the page in the tab network of browser.

  • @LINQ, it’s true. You’re right. The page appears and everything else. And the relaxation was just force of expression.

Show 1 more comment

1 answer

2


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

The method load, probably makes a GET request to the server, via Xmlhttprequest. That is, you are trying to make the call from action just the way you’re trying to block using this attribute.

The only output is to remove this attribute.

If you want to block the user from seeing the content of partial view by 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();
}
  • LINQ, There’s another way to block ?

  • 1

    There are some ways, you need to see what you think is best. You can inject the modal code when loading the page and then open the modal using the code that already exists on the page, that is, if no server-side processing is required. This modal is from bootstrap?

  • yes it is of the bootstrap.

  • 1

    So I think this is a viable output. Load the partial HTML into the main view and call the modal from this code. I don’t know how to do it because I know very little about bootstrap, but I think I understand the main idea.

  • Example: if (Request.IsAjaxRequest() == false) { return HttpNotFound();}

  • 1

    @Matheusmiranda Editei

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.