Force Action Method can only be called from another action method

Last post 06-04-2009 2:15 PM by levib. 7 replies.

Sort Posts:

  • Force Action Method can only be called from another action method

    06-01-2009, 5:56 PM
    • Member
      132 point Member
    • lenocin
    • Member since 05-20-2009, 5:55 AM
    • Everywhere
    • Posts 127

    I got an action method (say public void ShowMeSomething ), and I do not want users to be able to browse to mysite/this/showmesomething.  However I want this action method to be callable from another action method.

     Can one do this?  I guess not, because this action method will return a view.  Thought I'd ask anyhow

    “Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.”
    (Rich Cook)
  • Re: Force Action Method can only be called from another action method

    06-01-2009, 9:33 PM
    • Contributor
      5,066 point Contributor
    • Paul Linton
    • Member since 04-30-2008, 3:16 AM
    • Posts 883

    Make ShowMeSomething be private rather than public.

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: Force Action Method can only be called from another action method

    06-01-2009, 9:38 PM
    • Contributor
      4,601 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 792

    Paul is right - making the method private / internal will prevent it from being web-callable.  If you need the method to be public (for testability or whatever else), then decorate it with the [NonAction] filter.  This will make any public method on a controller class non-web-callable.

  • Re: Force Action Method can only be called from another action method

    06-01-2009, 9:45 PM
    • Member
      132 point Member
    • lenocin
    • Member since 05-20-2009, 5:55 AM
    • Everywhere
    • Posts 127

     Ok, I will try that.  But I'm not sure my "idea" is possible

    mysite/candidate/candidatedemo should not work.

    only way the above view render is from the controller: 

    // GET: /Candidate/
    public ActionResult Index()
    {
    	return RedirectToAction("CandidateDemo");
    }
    
    // GET: /Candidate/CandidateDemo
    private ActionResult CandidateDemo()
    {
    	return View("CandidateDemo");
    }
      let me try what you suggested.
    “Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.”
    (Rich Cook)
  • Re: Force Action Method can only be called from another action method

    06-01-2009, 9:45 PM
    Answer
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228

    You could mark the method as protected (the default is to only use public methods as candidates for actions), but I'd also consider moving the logic into another class that is not a controller itself, but that your controller will use.

     

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: Force Action Method can only be called from another action method

    06-01-2009, 10:04 PM
    • Member
      132 point Member
    • lenocin
    • Member since 05-20-2009, 5:55 AM
    • Everywhere
    • Posts 127

     Nope, dont work.  Obviously I tried shooting at something that's not there.  Big Smile

    I'll use some different logic to achieve the same result (partial view in a if block perhaps).  But first will try out what bitmask suggested.  Sounds interesting.

    Thanks for the time!

    “Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.”
    (Rich Cook)
  • Re: Force Action Method can only be called from another action method

    06-04-2009, 1:22 PM
    • Participant
      938 point Participant
    • CW2
    • Member since 05-28-2007, 5:38 PM
    • Czech Republic
    • Posts 207

    In addition the the solutions already suggested above:

    You may be able to modify your route definitions so that ShowMeSomething action does not match any, hence the URL will not be handled,

    Or you can decorate the action method with [NonAction] attribute - then it can remain public, action invoker will ignore it.

  • Re: Force Action Method can only be called from another action method

    06-04-2009, 2:15 PM
    Answer
    • Contributor
      4,601 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 792

    CW2:
    You may be able to modify your route definitions so that ShowMeSomething action does not match any, hence the URL will not be handled,

    Please do not do this.  First, it is somewhat difficult to prove that an action method can never be matched by any route.  Second, if you're trying to protect the action method, then protect the method itself (either through a filter or by making it non-public), not by constraining some particular MVC entry point that indirectly calls that method.

    Finally and most importantly (and this is related to the second point), there's nothing stopping the dev team from in the future adding a special well-known route like /ExecuteMVC.asmx?controller=Home&action=Index&responseType=RSS.  If we did implement this, it would bypass your route table entirely, and if your route is responsible for security decisions then your application is vulnerable.

    These reasons are among the many why we've been telling developers that you must not make security decisions in the routes.

Page 1 of 1 (8 items)