简体   繁体   English

命名空间问题

[英]Namespace Problem

Normally we all do use using System.Linq; 通常我们都using System.Linq; and using System.Data.Linq; using System.Data.Linq; for example on the code-behind and expect we can reach the members of these namespaces from Source Code like <%= Something.First()%> but when I wrote it, asp.net said it couldn't find First() in the context and I had to add <%@ Import Namespace="System.Linq" which looked very weird to me but it worked out. 例如在代码隐藏,并希望我们能够达成这些命名空间从源代码等的成员<%= Something.First()%>但是当我写的,asp.net表示无法找到First()的在上下文中,我不得不添加<%@ Import Namespace="System.Linq" ,这对我来说似乎很奇怪,但是可以解决。 Since they are targeting at the same class why they both need separate namespace importing. 由于它们都针对同一类,因此为什么它们都需要单独的名称空间导入。

Code-behind : 后台代码:

using System;
using System.Data.Linq;
using System.Linq;
using System.Text

namespace Something
{
   class Items : System.Web.UI
   {
       //...
   }

}

but also I need to add the same Linq namespace on the Html Source part 但是我还需要在Html Source部分上添加相同的Linq命名空间

<%@Import Namespace="System.Linq"%>

Do I know something wrong or this is some kind of bug in asp.net. 我是否知道某些错误,或者这是asp.net中的某种错误。 I thought when the page is compiling, asp.net combines these two classes and converts html source code into cs class and indicates the control in Control c= new Control(); 我以为在编译页面时,asp.net会结合这两个类并将html源代码转换为cs类,并在Control c= new Control();指示该控件Control c= new Control(); hierarchy. 层次结构。

Thanks in advance. 提前致谢。

Ps : I am trying to reach for example First() in Items.aspx and everything I mentioned about an asp.net page which is Items.aspx 附言我试图到达Items.aspx中的First() ,以及我提到的关于asp.net页面的所有内容,即Items.aspx

Check your web.config file for a namespaces section and make sure System.Linq is listed there. 检查您的web.config文件中的“名称空间”部分,并确保其中列出了System.Linq。

Documentation: 文档:
http://msdn.microsoft.com/en-us/library/ms164642.aspx http://msdn.microsoft.com/en-us/library/ms164642.aspx

You must specify your namespaces in both places. 您必须在两个地方都指定名称空间。 It's normal behavior. 这是正常行为。 That's needed by the compiler in order to pre-compile the aspx page and the code-behind page separately, before merging them into one class and doing the actual compilation. 编译器需要这样做,以便在将它们合并为一个类并进行实际编译之前,分别对aspx页和代码隐藏页进行预编译。

By default, a few common namespaces are already included in the aspx page, so you don't need to import them. 默认情况下,aspx页面中已经包含了一些常见的名称空间,因此您无需导入它们。 But in your case you need to import Linq. 但是在您的情况下,您需要导入Linq。

EDIT: And as Joel Coehoorn said, you can add to that list of default namespaces in Web.config, should you not want to manually add them in the aspx pages. 编辑:正如Joel Coehoorn所说,如果您不想在aspx页面中手动添加默认名称空间,则可以将其添加到该默认名称空间列表中。

First is not a method on the class, but an extension method defined in the System.Linq namespace. First不是类上的方法,而是System.Linq命名空间中定义的扩展方法。 Even though you may also use this extension method within the code behind, this doesn't mean that the ASP.NET compiler can find the extension method without a hint - hence the <%@ Imports ... %> directive. 即使您也可以在后面的代码中使用此扩展方法,但这并不意味着ASP.NET编译器可以在没有提示的情况下找到扩展方法-因此, <%@ Imports ... %>指令。

Note that the ASP.NET compilation (ie of the aspx) is separate from the compilation of the code behind. 请注意,ASP.NET编译(即aspx的编译)与后面代码的编译是分开的。 The latter runs when you build the project; 后者在您构建项目时运行; the former runs when you either access the page for the first time, or pre-compile it using "Publish..." or a web deployment project. 前者在您首次访问该页面或使用“发布...”或Web部署项目对其进行预编译时运行。 Hence each compiler needs to be told where to find this extension method if you use it in both places. 因此,如果您在两个地方都使用此扩展方法,则需要告知每个编译器在哪里可以找到此扩展方法。

我不会用First在标记,如果你仍然想这样做,使你的代码的包装背后,像SomeMethodSomeProperty和标记访问它的<%=SomeProperty %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM