To develop novel strategies for fabricating metal carbon-based materials with desirable compositions and structures for catalysis is of great importance. Here we propose for the first time the utilization of metal ionic liquid as a precursor for the synthesis of metal carbon-based materials through a one-step pyrolysis route. The method produces the metal dispersed cross-linked carbon framework with a highly mesoporous structure. Such a Ni/C-N catalyst exhibits outstanding electrocatalytic activity for CO2 reduction reaction, i. e., an excellent CO faradaic efficiency of approximately 94 % at an potential of -0.75 V (versus the reversible hydrogen electrode). The method proposed in this work is simple, convenient, low-cost, adjustable and versatile for the synthesis of different kinds of M/C-N materials because the structures and properties of metal ionic liquids can be easily designed and tuned by changing the anions and cations.