Is it ethical/acceptable to take courses for the sole purpose of getting a reference letter? I am interested in going back for my masters.  While I have been accepted by a masters program (quite a while ago) previously, I am looking at another university and require a fresh set of references.  I have lost track of my previous advisors and am unable to get work references.

So I need to prove myself at this point to somebody.  There are some optional courses in the undergrad that I am interested in, but to be honest I could just read the textbook and save some money.  

So:
Is it ethical/acceptable to take courses for the sole purpose of getting a reference letter?

and

Should I inform the prof before taking the class of my intent if this is a gray area?

Note:  I know that it is still the prof's choice if I get a letter and this method is nowhere near ideal.  Just the best I could come up with.
Students have lots of reasons for taking courses and I don't think I want to be in the business of telling students that there are right and wrong reasons to take a class. I certainly think it's a good idea for prospective PhD students to think about who might be good letter writers early on in their program and to make sure they work with some of them — not doing so can make the academic job market very difficult. I don't see this as being substantively different. If you take the class, do well, and then ask for a letter, you might get a good one. Or you might not. So I'd say go ahead.

That said, there is something very mercenary about the way you have phrased your question that makes me skeptical. Do you really think that working with faculty wouldn't teach you anything? If it's true that the only reason you want to take a class is because of the letter you might get, something seems off. Maybe your interests are different enough from the professor's that I'm skeptical that this person would be such a great letter writer for what you want to do.

Being able to excel in a Masters program means not resenting the idea that you may have to learn from professors when you could "just read the book." I think it's OK to take a class for a recommendation but you should use this as opportunity to learn something you are passionate about in ways you could not with just the textbook.

Talk to the professor. Let them know you are an advanced and passionate student. Get feedback on your work. Go beyond what is just in the book. If you do that, you will get more than you would otherwise. In the process, you will also earn a much stronger letter. If you can't get over that hump, I suspect that your recommendations — and your experience in grad school — will underwhelm.