问题
I usually include my namespaces e.g.
using myProject.Model;
to my .cs files but when i want to iterate an object derived from myProject.Model in my .aspx file i must always include the directive
<%@ Import Namespace="myProject.Model" %>
Why is that? I mean if it is declared in code behind file (in default.aspx.cs) of default.aspx Why should i add it again? Why it is not available?
Just wondering but i would like to know why is that.
Thank you in advance!
回答1:
You can add a namespaces element to your web.config to specify namespaces that you'd like to be imported automatically.
回答2:
Because the ASPX pages are compiled using a different "WebPage" compiler, you must specify the namespaces for each page on wich you requrire certain features form that namespace.
回答3:
Because the @Import directive allows you to specify the namespaces to be imported for the Asp.Net pages or user controls, while import keyword is used to specify the namespaces to be imported for the codebehind.
来源:https://stackoverflow.com/questions/12425114/why-should-i-include-the-namespace-in-each-aspx-page