简体   繁体   English

将CodeFile转换为CodeBehind

[英]Converting CodeFile to CodeBehind

So I seem to have some difficulties with making my CodeFile reference into CodeBehind. 所以我似乎在将CodeFile引用到CodeBehind时遇到了一些困难。 Before you ask, I do not have Visual Studio and I'm not on Windows. 在你问之前,我没有Visual Studio,我不在Windows上。 The code is being hosted on an IIS server that I have access to. 代码托管在我有权访问的IIS服务器上。

<% @Page Language="C#" Inherits="corn.cheese.pies.CasLogin" CodeFile="anon.cs" %>

This code works perfectly, however upon changing it to: 此代码完美无缺,但在将其更改为:

<% @Page Language="C#" Inherits="corn.cheese.pies.CasLogin" CodeBehind="anon.cs" %>

The page returns an error: 该页面返回错误:

Parser Error Message: Could not load type 'corn.cheese.pies.CasLogin'.

Are you sure you understand the difference between CodeFile and CodeBehind? 你确定你理解CodeFile和CodeBehind之间的区别吗? See, for example, CodeFile vs CodeBehind (ignore the accept answer, though, both links are dea) or this article about the difference. 例如,参见CodeFile vs CodeBehind (忽略接受答案,但两个链接都是dea)或本文关于差异。

In a nutshell: 简而言之:

A CodeBehind file is assumed to be compiled into an assembly that is deployed in the bin folder of your web site. 假定CodeBehind文件被编译到部署在您网站的bin文件夹中的程序集中。 When the ASP.NET engine loads your aspx file, it uses the Inherits directive to try to find that class. 当ASP.NET引擎加载您的aspx文件时,它使用Inherits指令来尝试查找该类。 The CodeBehind file is primarily used to aid compiling (and tooling, eg "View Source") but the work is done at compile time. CodeBehind文件主要用于辅助编译(和工具,例如“查看源代码”),但工作在编译时完成。

A CodeFile file is located and compiled by ASP.NET at runtime, on demand. ASP.NET会在运行时根据需要定位和编译CodeFile文件。 You can "pre-compile" your web site to fall back on the older model, which is useful if you won't want to deploy your source code to every web site. 您可以“预编译”您的网站以回退到较旧的模型,如果您不想将源代码部署到每个网站,这将非常有用。 But by default, your CodeFile file is a .cs file that is deployed to the site, and the class is generated in the ASP.NET temporary storage folder. 但默认情况下,您的CodeFile文件是部署到站点的.cs文件,该类是在ASP.NET临时存储文件夹中生成的。 (From a technical perspective: CodeFile pages are partial class definitions, with only the custom code-behind part of the class actually deployed in the C# file; this is why CodeFile didn't exist initially, because the C# compiler could not initially do partial classes.) (从技术角度来看: CodeFile页面是partial class定义,只有partial class的自定义代码隐藏部分实际部署在C#文件中;这就是CodeFile最初不存在的原因,因为C#编译器最初不能做部分类。)

By changing from CodeFile to CodeBehind you are telling ASP.NET to ignore the presence of the C# file, and only look for it in the compiled assembly. 通过从CodeFile更改为CodeBehind您告诉ASP.NET忽略C#文件的存在,并且只在已编译的程序集中查找它。 ASP.NET is, in turn, telling you that the compiled assembly doesn't actually contain the class you told it to look for. 反过来,ASP.NET告诉您编译的程序集实际上并不包含您告诉它要查找的类。

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

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