[verb た-form] + とおり(に) [verb (dictionary form)] + とおり(に) [noun] + の + とおり(に) [noun] + どおり(に) この / その / あの + とおり(に)
とおりに means "the way", "in line with": you do something exactly as a model, instruction or expectation says. It follows a verb in the た-form or dictionary form, or a noun with の, and comes before the verb it describes.
The た-form follows something already said, shown or seen. The dictionary form follows something that holds now or in general.
に is there because the phrase describes how a verb is done. When nothing follows but だ or です, or when とおり opens the sentence with a comma, there is no に: what happened matches what was said, planned or expected.
With この and その the same split between とおりに and とおり holds. このとおりにしてください points at something you are showing: "do it exactly like this". そのとおりです agrees with what someone has just said: "that's right".
Before a noun, とおり takes の.
A noun can also take とおり directly, without の. It is then voiced to どおり. Either form can be written in kanji, とおり通り, and after a noun it usually is.