LONGMONT, Colo. — This northern Colorado city vaulted onto the front lines of the battle over oil and gas drilling two years ago, when residents voted to ban hydraulic fracturing from their grassy open spaces and a snow-fed reservoir where anglers catch smallmouth bass.

But these days, Longmont has become a cautionary tale of what can happen when cities decide to confront the oil and gas industry. In an aggressive response to a wave of citizen-led drilling bans, state officials, energy companies and industry groups are taking Longmont and other municipalities to court, forcing local governments into what critics say are expensive, long-shot efforts to defend the measures.

While the details vary — some municipalities have voted for outright bans, and others for multiyear suspensions of fracking — energy companies in city after city argue that they have a right to extract underground minerals, and that the drilling bans amount to voter-approved theft. They also say state agencies, not individual communities, are the ones with the power to set oil and gas rules.

Because the cases are being fought one by one at the state level, they are not expected to set any immediate nationwide standard on whether homeowners and local leaders have the power to keep drilling rigs out of their towns. But they are being watched as legal litmus tests as more governments plunge into the acrimonious debate over fracking, the process of pumping huge amounts of water, sand and chemicals underground to release oil and gas buried in shale rock.