我正在尝试使用 Global.asax 文件,但失败得很惨。代码中的每一行都有错误,但有趣的是,不管有多少行、行包含什么等等,它总是报告错误。这让我觉得配置设置不正确但不知道从哪里开始。
该项目是一个网站而不是 Web 应用程序,但我阅读了 Global.asax 并且在网站中使用它似乎是可以接受的。在编写项目所需的代码之前,我提供了用于测试 Global.asax 是否在项目中工作的代码。
<%@ Application Language="C#" %>
<script runat="server">
static string _pagePath;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
_pagePath = Server.MapPath("~/Folder/Page.aspx");
}
// ...
void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.PhysicalPath;
if (path == _pagePath)
{
Response.Write("Page viewed");
Response.End();
}
}
</script>
我还提供了一个出现的示例错误消息列表
'Class' statement must end with a matching
'End Class' 'If', 'ElseIf', 'Else', 'End If', 'Const', or 'Region' expected
'Namespace' statement must end with a matching 'End Namespace'
Statement cannot appear outside of a method body/multiline lambda.
希望有人能找出我做错了什么,这样我就可以使用 Global.asax。非常感谢任何帮助!
最佳答案
将 Language="C#"属性添加到 global.asax 顶部的指令。右键单击该文件,选择查看标记,添加属性并重建。
<%@ Application Codebehind="Global.asax.cs" Inherits="GLOBAL CLASS" Language="C#" %>
关于C# ASP.Net - Global.asax 充满错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265655/