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).

4
  • Is this Web Forms, or MVC? Commented Oct 13, 2015 at 16:05
  • Its Web forms, sorry I assumed that would be assumed from Web pages Commented Oct 13, 2015 at 19:10
  • 1
    Web Pages is actually an entirely separate technology within ASP.NET. Web Pages uses Razor syntax to create pages. It's like MVC without controllers. So Web Forms is important to specify. Commented Oct 13, 2015 at 19:12
  • Okay thanks. I've updated the tags Commented Oct 13, 2015 at 19:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

That question is mine. The references are not visible in that dialog, which is weird because VS recommends importing them!
Try manually adding the namespace name in the TextBox just before Add User Import button. I hope you gave a visit to 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.
It worked! Amazingly simple, but bizarre that VS doesn't see potential References like it does in code...
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" />

1 Comment

It's not razor it's Web forms that's why I don't understand why it is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.