In victory, Mr. Brat said that his candidacy had resonated with voters who believed that politics had been dumbed down by partisan infighting.

“The American people want to pay attention to serious ideas again,” Mr. Brat said, speaking on Fox News. “Our founding was built by people who were political philosophers, and we need to get back to that, away from this kind of cheap political rhetoric of right and left.” He will face Jack Trammell, a Democrat who is also a professor at Randolph-Macon, this fall in the heavily Republican district.

Republicans were so sure that Mr. Cantor would win that most party leaders had been watching for how broad his victory would be. His defeat will reverberate in the capital and could have major implications for any chance of an immigration overhaul.

Mr. Cantor, 51, who is in his seventh term, had sought to counter Mr. Brat’s accusations that he was too willing to compromise on immigration. The majority leader, who had raised $5.4 million for the campaign, blanketed Virginia’s Seventh Congressional District with fliers and television advertisements in which he emphasized that he opposed an “amnesty” policy.