Why should i include the namespace in each aspx page?

橙三吉。 提交于 2020-01-05 03:16:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!