The ability to perceive temperature is crucial for most animals. It enables them to maintain their body temperature and swiftly react to noxiously cold or hot objects. Caenorhabditis elegans is a powerful genetic model for the study of thermosensation as its simple nervous system is well characterized and its transparent body is suited for in vivo functional imaging of neurons. The behavior triggered by experience-dependent thermosensation has been well studied in C. elegans under temperature-gradient environments. However, how C. elegans senses temperature via its nervous system is not well understood due to the limitations of currently available technologies. One major bottleneck is the difficulty in creating fast temperature changes, especially cold stimuli. Here, we developed a microfluidic-based platform that allowed the in vivo functional imaging of C. elegans responding to well-controlled temporally varying temperature stimulation by rapidly switching fluid streams at different temperatures. We used computational models to enable rational design and optimization of experimental conditions. We validated the design and utility of our system with studies of the functional role of thermosensory neurons. We showed that the responses of PVD polymodal nociceptor neurons observed in previous studies can be recapitulated. Further, we highlighted how this platform may be used to dissect neuronal circuits with an example of activity recording in PVC interneurons. Both of these neuron types show sensitization phenotypes. We envision that both the engineered system and the findings in this work will spur further studies of molecular and cellular mechanisms underlying cold-sensing through the nervous system.