Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Up to .Net 4.5...

There is a web.config file in the Views folder, you can add namespaces in there, that is global to all views:

e.g:

<namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Globalization" />
        <add namespace="My.Library" />
</namespaces>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top