I just realized that Razor has different processing order.

It is common that we use htmlHelper.ViewContext.HttpContext.Response for rendering output via ASP.NET MVC’s HtmlHelper. We can also use htmlHelper.ViewContext.Writer to achieve the same result.

This is not the case with ASP.NET MVC 3’s new Razor view engine. If we use Response object directly, the output will be rendered in the top of the page. Using ViewContext.Writer will solve this issue.