ASP.NET MVC: an action that is called only by other actions

I want to make a GET action that runs only if it is called by another action, so if the user does not type the URL in the address bar. What can I check to determine if this is so?

+3
source share
3 answers

You can decorate an action with ChildActionOnlyAttribute .

  [ChildActionOnly]
  public ActionResult Menu() {
    var menu = GetMenuFromSomewhere();
      return PartialView(menu);
  }

You can then use the RenderAction () and Action () Html helpers, as usual, and the action cannot be called at the URL.

They are new to MVC 2, but from the tags, I assume you are already using this.

Example: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

+15

acces private. , URL-:

private ActionResult PrivateAction()
{
    return View("SomeView");
}

:

public ActionResult SomeAction()
{
    if(someCondition)
        return PrivateAction();
}
+12

@Andras Framing actions with help [ChildActionOnly]prevents the action from being called through ajax and is necessary in some scenarios. About @ajbeaven, ask the question correctly, but in an ajax call situation, I find it best to decorate the action with an attribute [HttpPost].

Luck

0
source

Source: https://habr.com/ru/post/1786511/


All Articles