Question: [![solenoid](https://i.stack.imgur.com/zyfYR.gif)](https://i.stack.imgur.com/zyfYR.gif)  

(source: [gsu.edu](http://hyperphysics.phy-astr.gsu.edu/hbase/magnetic/imgmag/sol.gif))

To calculate the magnetic field, a rectangle amperian loop was drawn, and since the sides of the rectangle are perpendicular to the magnetic field, and the top is too far away to have any field lines cross it, only BL makes a contribution.

[![equation](https://i.stack.imgur.com/PVrPb.gif)](https://i.stack.imgur.com/PVrPb.gif)  

(source: [gsu.edu](http://hyperphysics.phy-astr.gsu.edu/hbase/magnetic/imgmag/sol2.gif))

I don't understand why this magnetic field is only contained within the solenoid. Shouldn't the magnetic field at any point on the amperian loop equal μnI, according to Ampere's law?

Answer: 
Solenoids of finite length do have a non-zero magnetic field outside. As the length increases, this magnetic field is significantly smaller than the magnetic field inside the solenoid. An ideal solenoid has infinite length and this magnetic field is zero. One is usually dealing with this idealisation. This is similar to assuming that there is no electric field outside a parallel plate capacitor. It is a fun (not necessarily easy) problem to estimate the ratio of the interior and exterior magnetic fields for a finite solenoid.
You're on the right track. Now consider a second Amperian loop, shaped like the first, but with the bottom *outside* the solenoid. The current passing through this loop is zero, and you've already decided that the far end of the loop doesn't contribute, so you must conclude that $B\_{out} = 0$. 

You could do the same thing with a rectangular Amperian loop with the top and bottom at $s\_{t}$ and $s\_{b}$, respectively, where $s$ is the distance from the axis. If $s\_{t}$ and $s\_{b}$ are both outside the cylinder (and assuming $B$ is a function of $s$, which you may deduce from symmetries) you would find $B\_{out}(s\_{b})L - B\_{out}(s\_{t})L = 0$. Thus, $B\_{out}$ must be constant, and the requirement that it goes to zero far away means that the constant must be zero.