HARRISBURG, Pa. — Republican U.S. Sen. Pat Toomey will not get an endorsement from the National Rifle Association, and his rating dropped by two grades after he went against gun rights groups and his party leadership to vote for an expansion of background checks.

The Pennsylvania senator’s new C rating is a downgrade from the A rating and hearty endorsement the NRA gave him when he last ran in 2010 in the Democratic-leaning state, whose residents are passionate about hunting and guns.

Toomey is running in a hotly contested race that could determine control of the Senate. His Democratic challenger, Katie McGinty, favors a much broader range of gun control measures than he does.

Finding compromise on gun control became something of a signature issue in Toomey’s first term. And despite the fact that Toomey voted in line with the NRA and gun rights groups on various other gun control measures, he is now the rare conservative Republican to run with the endorsement of two prominent gun control activists, billionaire independent former New York mayor Michael Bloomberg and former Democratic congresswoman Gabby Giffords.

An NRA spokeswoman said Thursday that Toomey’s latest rating is based on his voting record, including the legislation to require background checks for online gun sales and purchases at gun shows. Even though the organization is not renewing its endorsement of Toomey, spokeswoman Jennifer Baker attacked McGinty.

McGinty would be a “rubber stamp for an anti-gun Supreme Court justice” who would vote to overturn the Heller decision, Baker said, referring to the high court’s 2008 decision that determined Americans have the right to keep and bear arms for self-defense.

McGinty’s campaign spokesman, Sean Coit, replied that McGinty “wants to pass common-sense gun safety laws. Pat Toomey and the NRA don’t. … The NRA attacking Katie tells you all you need to know about which candidate is on the side of gun safety in this race.”

McGinty drew an F rating from the NRA, unsurprising given her support for gun control measures including banning the sale of military-style weapons and imposing a federal limit on magazine capacity. McGinty is endorsed by the gun control group CeaseFire Pennsylvania.

The background checks bill failed most recently 50-48 in December, with just four Republicans, including Toomey, backing it and all but one Democrat supporting it.

The NRA is the latest in a growing line of gun rights groups that have declined to endorse Toomey. For Toomey, it may help him with a Republican’s perennially thorny task of appealing to moderates in a state where Democrats outnumber Republicans by a 4-3 ratio. But it could hurt him with the Republican base.

Toomey has defended his voting record, saying he sees no conflict between the right to own a gun and “a three-minute background check.”

“As with many issues, we will only achieve progress on commonsense gun safety measures if we work to build bipartisan consensus, rather than talk past each other with more partisanship,” the Toomey campaign said.

Currently, the checks are only required for transactions from licensed gun dealers.