Republicans testing their 2020 strategy to retake the House scored a victory on Tuesday, narrowly clinging to a traditionally red seat in a fiercely fought special election.

Dan Bishop, a Republican state senator who ran for Congress as President Donald Trump’s ideological twin, beat Democrat Dan McCready — but not without the aid of millions in outside money and a last-minute Trump rally in a district the president won by 12 points in 2016. Bishop won by 2 percentage points on Tuesday, and less than 4,000 votes.

His victory ends what was essentially the longest contest of the 2018 midterm elections after stunning allegations of electoral fraud forced the state to invalidate the results of November’s race and hold a special election. Bishop, a bomb-throwing state legislator, won the Republican primary in May. But McCready, who was the Democratic nominee last year, has just wrapped up an unheard-of 27-month campaign.

Republicans are greeting the victory as a sign that the blue wave that crashed through suburban strongholds in the midterms is receding.

“Tonight was the first step toward taking back the House of Representatives in 2020,” Bishop said in his victory speech.

If this race — for all its singular drama — offers any prediction about next year’s elections, it’s that Trump’s strength with the Republican base may be enough in some suburban districts to overcome Democrats’ fervor and organizing edge.

Bishop pitched himself to voters as a loyal Trump foot soldier. “I think defending the president — and being vocal about that — will be a role I intend to take immediately,” he said.

He tightly embraced Trump’s harsh anti-immigrant positions, and an early TV ad from his campaign linked McCready to progressive Democrats it dubbed “crazy liberal clowns”: Reps. Alexandria Ocasio-Cortez (N.Y.) and Ilhan Omar (Minn.), and Sen. Bernie Sanders (I-Vt.).

The approach was a preview of Republicans’ blueprint for retaking the House in 2020 — and of its limitations. North Carolina’s 9th Congressional District is heavily populated by conservative white voters who supported Trump in 2016 and whom Republicans plan to turn out in 2020 with explicit calls to defend the president. But the district is also home to a significant number of Black and Native American voters who traditionally support Democrats, plus a small group of college-educated white suburbanites of the kind who fled Trump and Republicans in the midterms.

Polls taken in the last weeks of the race showed Bishop and McCready essentially tied, prompting Trump and Vice President Mike Pence to hold last-minute rallies for Bishop on election eve. “To stop the far left, you must vote in tomorrow’s special election,” Trump said Monday night at a Fayetteville rally.

But Bishop, in a possibly nervous departure from his script, closed the race by talking up his “lengthy record of bipartisanship” and attacking McCready’s record in business rather than trying to yoke him to progressives in Congress.

McCready’s offer to voters was contrastingly sober. The Marine Corps veteran who became a solar energy investor spent most of the campaign talking about prescription drug prices and health care premiums — in short, a continuation of the campaign Democrats ran on and won with in Republican-leaning suburbs in 2018.

What set him apart is that, thanks to staggering claims of electoral fraud involving his former opponent, McCready’s campaign lasted nearly a year longer than he had bargained for.

On election night in November 2018, McCready believed he’d lost to pastor Mark Harris by 905 votes, conceded and left for a family vacation to Disney World. But Harris never made it to Congress. Credible allegations that a Republican contractor illegally submitted hundreds of mail-in ballots in order to steal the race forced the state board of elections to order an unprecedented new election. Harris, after questions arose about his knowledge of the scheme, bowed out of a rematch.

The new election meant McCready was in for 10 more months of campaigning. As of Tuesday night, he had campaigned 27 months, making his campaign older than his youngest daughter — who just turned 2.

Bishop distinguished himself in a 10-way primary as the field’s most bombastic conservative. He is the author of North Carolina’s infamous “bathroom bill,” which targeted transgender people by mandating they use public facilities corresponding to the sex on their birth certificates. National Republican groups poured millions into the primary, including groups hoping to make a woman, Leigh Brown, the face of the party’s attempts to maintain its grip on the district.

“The answer is a ‘Who knows?’” said Julie Conway, the president of a group, VIEW PAC, which seeks to elect more Republican women and supported Brown, asked shortly before the election if she thought Brown would have fared better. “But clearly Bishop isn’t lighting it on fire.”

His campaign against McCready unleashed a geyser of outside spending and fundraising by both political parties. Republicans and outside groups mounted a deluge of negative television ads. McCready, who raised roughly $3 million more than Bishop; the Democratic Congressional Campaign Committee; and allied groups spent millions on data and organizing efforts to turn out traditionally Democratic voters, such as college students, Blacks and Native Americans.

McCready’s message to those voters: Don’t let them silence you.

“Our right to vote is our most sacred right as Americans,” he said recently. “It’s a right that people have fought for here at home in the battle for civil rights. And that was stolen from the people.”

This article has been updated to include final results and comments.