5. Given $a, b \in \mathbf{R}$, the function $f(x)=a x-b$. If for any $x \in[-1,1]$, we have $0 \leqslant f(x) \leqslant 1$, then the range of $\frac{3 a+b+1}{a+2 b-2}$ is
A. $\left[-\frac{1}{2}, 0\right]$
B. $\left[-\frac{4}{5}, 0\right]$
C. $\left[-\frac{1}{2}, \frac{2}{7}\right]$
D. $\left[-\frac{4}{5}, \frac{2}{7}\right]$
Solve:
$\frac{3 a+b+1}{a+2 b-2}=\frac{3\left(a+\frac{4}{5}\right)+\left(b-\frac{7}{5}\right)}{\left(a+\frac{4}{5}\right)+2\left(b-\frac{7}{5}\right)}$, let $u=\frac{b-\frac{7}{5}}{a+\frac{4}{5}} \Rightarrow \frac{3 a+b+1}{a+2 b-2}=\frac{3+u}{1+2 u}=\frac{1}{2}+\frac{\frac{5}{2}}{1+2 u}$.
And $0 \leqslant f(x) \leqslant 1 \Leftrightarrow\left\{\begin{array}{l}-1 \leqslant a+b \leqslant 0, \\ -1 \leqslant b-a \leqslant 0 .\end{array}\right.$
The feasible region is shown in the figure. Since $k_{P A}=-\frac{19}{3}, k_{P B}=-\frac{19}{13}$,
Therefore, $\frac{1}{2}+\frac{\frac{5}{2}}{1+2 u} \in\left[-\frac{4}{5}, \frac{2}{7}\right]$, so the answer is $D$.