Where can the using directive be placed in ASP.NET Razor pages?

Jez picture Jez · May 21, 2013 · Viewed 7.6k times · Source

I've traditionally always put @using directives in my ASP.NET Razor pages at the top, along with the @model directive. However, for my overall layout, I want to make sure the DOCTYPE declaration is at the very beginning of the document, so I want to push the @using down a bit. Would you following be valid?

<!DOCTYPE html>
<html>
    @using My.Library;
    <head>
        <title>Test web page</title>
        ...

Also, is there any documentation on where the @using directive can be used in Razor pages? I can't seem to find any. Is it valid to use it after some other Razor code, for example, or does it have to appear first?

Answer

Majid picture Majid · May 21, 2013

It is valid and you can use @using any where before that you need that library.

MSDN:

HTML markup lines can be included at any part of the code.

so you can put DOCTYPE at the top of page.