When he was 26, Joe McGinniss wrote “The Selling of the President 1968,” a landmark study of the uses of advertising in presidential campaigns. It stayed on bestseller lists for seven months, making Mr. McGinniss the youngest living author, up to that point, to have a No. 1 nonfiction bestseller.

At 40, he published “Fatal Vision,” a page-turning tale about Jeffrey MacDonald, an Army doctor who continued to maintain his innocence long after he was convicted of murdering his wife and two daughters.

“Fatal Vision” sold millions of copies, was made into an NBC miniseries and was hailed as a true-crime classic. But in later years, the book became the centerpiece of an impassioned debate about journalistic ethics, which came to overshadow Mr. McGinniss’s early reputation as one of the leading nonfiction authors of his generation.

Mr. McGinniss was 71 when he died March 10 at a hospital in Worcester, Mass. The cause was prostate cancer, his wife, Nancy Doherty, said.

He was still writing until shortly before his death, chronicling his struggle with cancer in Facebook updates and in an unfinished book, but in many ways he became better known for what people said about him than for what he actually put on the page.

Joe McGinniss, seen here in 1993, was the author of “The Selling of the President 1968” and “Fatal Vision,” a controversial book about the Jeffrey MacDonald murder case in the 1970s. (David Rogowski /For The Washington Post)

In 1968, Mr. McGinniss overheard an advertising executive say that his company had acquired the “Humphrey account.” Until that moment, Mr. McGinniss had not realized that presidential campaigns hired teams of advertisers to sell their candidates like a brand of soap.

When Democratic candidate Hubert H. Humphrey’s handlers turned down Mr. McGinniss’s request to peek behind the scenes, he approached the Republican candidate, Richard M. Nixon. Nixon’s people agreed to let him into the inner sanctum.

“This is the beginning of a whole new concept,” said one of Nixon’s top imagemakers, Roger Ailes, who later became the head of Fox News. “This is the way [presidents] will be elected forevermore. The next guys up will have to be performers.”

“The Selling of the President 1968” became a runaway bestseller when it was published in 1969 and was later made into a Broadway play.

The book irreverently pulled back the curtain on political marketing and heralded a promising career for its author, the youngest writer other than Anne Frank to top the nonfiction list up to that point.

Mr. McGinniss published two books in the 1970s, then journeyed to Alaska for “Going to Extremes,” his 1980 account of the dark side of the Alaskan dream.

In the late 1970s, Mr. McGinniss met MacDonald, a onetime Army doctor whose pregnant wife and daughter had been bludgeoned and stabbed to death in 1970 at their home in North Carolina.

They began on friendly terms, and Mr. McGinniss agreed to share up to a third of the profits from a book about MacDonald. The doctor said his family had been attacked in the middle of the night by a Charles Manson-like group of hippies, chanting “Acid is groovy.” But MacDonald was convicted of murder in 1979, and Mr. McGinniss came to believe he was a manipulative psychopath.

When “Fatal Vision” appeared in 1983, novelist Ross Thomas praised it in his Washington Post review as “an absorbing and totally damning indictment of Dr. Jeffrey MacDonald.”

Most reviewers agreed, but Christopher Lehmann-Haupt of the New York Times had one caveat in his otherwise laudatory review: “There are bound to be those readers who feel that Mr. McGinniss has exploited and betrayed a friendship.”

MacDonald sued Mr. McGinniss for $15 million, saying he had been betrayed by the author, and the case was ultimately settled out of court, with Mr. McGinniss’s publishers paying MacDonald $325,000 in return for MacDonald’s agreement that Mr. McGinniss had done nothing legally wrong.

But the case took another turn in 1989, when Janet Malcolm wrote a two-part series for the New Yorker, “The Journalist and the Murderer,” later published as a book. Her opening lines have been repeated in journalism seminars ever since:

“Every journalist who is not too stupid or too full of himself to notice what is going on knows that what he does is morally indefensible. He is a kind of confidence man, preying on people’s vanity, ignorance or loneliness, gaining their trust and betraying them without remorse.”

Malcolm dissected Mr. McGinniss’s entire career, saying she found evidence of deceit and a willingness to ingratiate himself with people he later betrayed. Others looked at the case, including filmmaker Errol Morris, who harshly criticized Mr. McGinniss in a 2012 book. Meanwhile, MacDonald continued a series of legal appeals of his conviction, all of which have been rejected in court.

Mr. McGinniss wrote a long, point-by-point rebuttal of Malcolm’s article, but in some ways he never overcame the suspicion that he had betrayed the trust of a source.

“Malcolm’s portrait of McGinniss is so damning and her portrait of MacDonald so noncommittal,” writer David Rieff wrote in the Los Angeles Times, “that one sometimes has to wonder about an approach in which a writer’s dishonesty is treated with more heat than the murder of three human beings.”

Joseph McGinniss was born Dec. 9, 1942, in New York, where his father ran a travel agency. According to Mr. McGinniss’s wife, his parents allowed him to choose his middle name when was a child. He selected Ralph, after his favorite baseball player, Ralph Kiner, who died Feb. 6.

Mr. McGinniss was a 1964 graduate of the College of the Holy Cross in Worcester, Mass., and worked at newspapers in Port Chester, N.Y., and Worcester before going to Philadelphia. In later years, he lived in Pelham, Mass.

His first marriage, to Christine Cooke, ended in divorce.

Survivors include his wife of 37 years, Nancy Doherty of Pelham, Mass.; three children from his first marriage, Christine Marque of Paris, Suzanne Boyer of Media, Pa., novelist Joe McGinniss Jr. of Washington; two children from his second marriage, Matthew McGinniss of Philadelphia and James McGinniss of Pelham; and seven grandchildren.

Mr. McGinniss wrote several more books about crime and sports, but the furor over “Fatal Vision” seemed to sap his strength as a writer. His 1993 biography of Sen. Edward M. Kennedy, “The Last Brother,” was widely derided for imagined dialogue and other deficiencies.

In 2010, while researching a biography on Sarah Palin, Mr. McGinniss rented a house next door to the 2008 Republican vice presidential candidate in Wasilla, Alaska, triggering outrage from her supporters. Todd Palin complained of Mr. McGinniss’s “creepy obsession with my wife.”

Regardless of the risks, Mr. McGinniss believed a writer had to dive into a story, to live in his subject’s world to report it with fidelity and understanding.

“For me,” he told The Post, “the only valid kind of writing is simply one guy telling you where he’s been, what he knows and feels.”