LINQ在global.asax中 [英] LINQ in global.asax
问题描述
好的,所以我咬紧牙关,尝试更多地使用LINQ.我正在创建一个Web应用程序,我希望该应用程序对象存储少量静态数据.它存储在XML文件中.麻烦的是,我无法使我的global.asax文件使用LINQ,因为我无法在其中的任何位置放置using语句而不会出现错误,这意味着我无法使用基本的LINQ语法. br/>
OK, so I am biting the bullet and trying to use LINQ more. I am creating a web app where I''d like the application object to store a small amount of static data. It''s stored in an XML file. Trouble is, I can''t get my global.asax file to use LINQ, because I can''t put a using statement anywhere inside it without getting an error, which means I can''t use the basic LINQ syntax.
<script runat="server">
using System.Linq;
void Application_Start(object sender, EventArgs e)
{
string path = Server.MapPath("~/appdata/breeds.xml");
}
这给了我一个错误.
看起来我真正的问题是,如何创建带有代码的global.asax文件? IDE不会执行此操作,并且手动执行此操作对我不起作用,我收到无法加载类型" MyNamespace.Global"错误.
我的Global.asax:
<%@ Application Codebehind ="Global.asax.cs" Inherits ="DIA.Global" Language ="C#"%>
global.asax.cs始于:
This gives me an error.
Looks like my real question is, how do I create a global.asax file with a code behind ? The IDE won''t do it, and doing it by hand is not working for me, I get a ''Could not load type ''MyNamespace.Global'' error.
My Global.asax:
<%@ Application Codebehind="Global.asax.cs" Inherits="DIA.Global" Language="C#" %>
The global.asax.cs starts with:
using System;
using System.Web;
namespace DIA
{
public class Global : HttpApplication
{
推荐答案
首先,您可以使用等价的using
语句,而无需进行代码隐藏:
First of all, you can use an equivalent ofusing
statements without codebehind:
<%@ Import Namespace="System.Linq" %>
...但是我个人更喜欢代码隐藏样式,因为它的工作方式与普通C#代码相同,并且具有更大的灵活性.
转换为代码隐藏
通过File-> New-> Website创建项目时,VS将创建一个网站,其中自动生成的默认项采用ASP.NET编码的非代码隐藏/脚本"样式.如果您更喜欢使用代码隐藏样式,则仍然可以轻松地转换为使用Global.asax的代码隐藏排列,方法是删除或重新添加,或者按照以下步骤操作:
1.以常规方式添加一个名为Global.asax.cs的类文件-如果您是通过网站"模板创建网站的,请确保您的类文件位于App_Code 中.
2.使类从HttpApplication继承.
3.将代码移到类文件中,然后将Codebehind和Inherits属性添加到<%@ Application%>中.在Global.asax中,如下面的示例所示.
如果您通过使用文件"->新建"->项目/解决方案"并选择一个Web应用程序模板来创建网站,则默认文件将通过代码隐藏编码样式生成.这是我通常喜欢的.
global.asax后面的示例代码
以下是代码隐藏样式global.asax和global.asax.cs的示例:
global.asax
...however I personally prefer the codebehind style because it works the same way as normal C# code, and has greater flexibility.
Converting to codebehind
When you create a project via File->New->Website, VS will create a website where the auto-generated default items are in the non-codebehind/"scripty" style of ASP.NET coding. If you prefer using the codebehind style, you can still easily convert to using the codebehind arrangement for Global.asax, either by deleting or re-adding, or by following these steps:
1. Add a class file the normal way, called Global.asax.cs - if you created your site via the Website template, make sure your class file goes in App_Code.
2. Make the class inherit from HttpApplication.
3. Move your code into the class file, and add the Codebehind and Inherits attributes to <%@Application %> in Global.asax, as I show in my samples below.
If you create a website by using File->New->Project/Solution and selecting one of the Web Application templates, the default files will be generated via the codebehind coding style. This is what I generally prefer.
Sample codebehind global.asax
Here is a sample of what a codebehind-style global.asax and global.asax.cs would look like:
global.asax
<%@ Application Codebehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>
global.asax.cs
global.asax.cs
using System;
using System.Web;
//as many using''s go here as you please ;)
namespace MyNamespace
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//code here...
}
//other events and methods...
}
}
好吧,我做了一些玩耍,发现了另一种方法...
global.asax
OK, I did some playing around and found another way of doing it...
global.asax
<%@ Application Language="C#" CodeFile="Global.asax.cs" Inherits="Global" %>
global.asax.cs (在项目的根目录中,而不是App_Code中)
global.asax.cs (in root of project, not App_Code)
using System;
using System.Web;
//using's here
public partial class Global : HttpApplication
{
/*code goes here... */
}
根据此 [
According to this[^], CodeBehind is deprecated and CodeFile is the correct attribute.
In VS2010, CodeFile isn''t even in the intellisense attribute list inside <%@Application %> - at least for me - which is why my first example used CodeBehind. That is the stupid part about this issue I guess. I remembered that there was another newer way, hence this answer...
在创建了新的ASP.Net Web应用程序之后,我通过右键单击该项目并选择以下内容来创建了global.asax文件:添加新项目...",然后选择全局应用程序类".我认为在拥有global.asax文件之后,它不会为您提供该选项,因此在修改内容之前,务必要做到这一点.以下是默认情况下给我的代码:
After creating a fresh ASP.Net web application, I created the global.asax file by right clicking the project, selecting add "New Item..." and selecting "Global Application Class". I don''t think it gives you that option after you already have a global.asax file, so it''s important to do it before you tinker with stuff. Here is the code behind it gave me by default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace WebApplication2
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
以及global.asax文件的内容:
And the contents of the global.asax file:
<%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication2.Global" Language="C#" %>
请注意,LINQ是默认包含的(还请注意,CP决定删除一些空行...因此该文件的外观与我复制时的外观不完全相同).
Notice that LINQ is included by default (also note that CP decided to take away some of the blank lines... so the file doesn''t appear exactly as it did when I copied it).
这篇关于LINQ在global.asax中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!