0

I am referencing a class library in a web application project (both in same solution). Within the web pages of the web application, if I do this:

If MyValidation.CorrectEmailFormat(email) Then ...

...Visual Studio 2013 underlines the method, and suggests I import MyCompany.EmailMethods at the top of the page. If I do the Import, the page compiles and the method works okay.

However, because these methods are used extensively across the application, I don't want to add them at page level every time. So I headed for web.config, and did this:

<pages>
    <namespaces>
        <add namespace="MyCompany.EmailMethods" />
    </namespaces>
</pages>

However, VS is still prompting me to perform the Import at the top of every page, and the method is not recognised in the page without doing this. What I am doing wrong please? I assumed from MSDN and other sources this was the correct way to achieve this.

Web application is ASP.Net web pages (4.6).

EvilDr
  • 8,943
  • 14
  • 73
  • 133
  • Is this Web Forms, or MVC? – mason Oct 13 '15 at 16:05
  • Its Web forms, sorry I assumed that would be assumed from Web pages – EvilDr Oct 13 '15 at 19:10
  • 1
    [Web Pages](http://www.asp.net/web-pages) is actually an entirely separate technology within ASP.NET. Web Pages uses Razor syntax to create pages. It's like [MVC](http://www.asp.net/mvc) without controllers. So [Web Forms](http://www.asp.net/web-forms) is important to specify. – mason Oct 13 '15 at 19:12
  • Okay thanks. I've updated the tags – EvilDr Oct 13 '15 at 19:14

2 Answers2

1

The reference must be added to the Imported Namespace as described in the following SO post

add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-project

It must be added in the project properties page at the bottom part titled Imported Namespaces

Community
  • 1
  • 1
haraman
  • 2,744
  • 2
  • 27
  • 50
  • That question is mine. The references are not visible in that dialog, which is weird because VS recommends importing them! – EvilDr Oct 13 '15 at 19:16
  • 1
    Try manually adding the namespace name in the TextBox just before Add User Import button. I hope you gave a visit to https://msdn.microsoft.com/en-us/library/3w4tctcf(v=vs.120).aspx . This page also says something about importing a namespace and a class in a namespace. – haraman Oct 13 '15 at 19:36
  • It worked! Amazingly simple, but bizarre that VS doesn't see potential References like it does in code... – EvilDr Oct 14 '15 at 09:19
0

The <pages> directive applies to ASPX files only.

You need to use the equivalent directive for Razor:

<system.web.webPages.razor>
    <pages>
        <namespaces>
            <add namespace="MyCompany.EmailMethods" />
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964