手动创建的 global.asax.cs 文件不起作用



我有网站项目,我已经开始开发api(至少在尝试(。

创建Global.asax文件时出现问题。由于这是Web Site Project,当我创建Global Application Class时,它只创建Global.asax而没有Global.asax.cs

经过一些研究,我发现我需要手动完成,所以我创建了Global.asax.cs文件,它会自动将其设置为我已经创建的Global.asax,我认为这很好。

问题是我认为我在 Global.asax.cs 中的代码没有运行。我尝试将断点放在Global.asax - Application_Start function里面,它停在那里(这意味着它正在工作(,但是当我在 Global.asax.cs 做同样的事情时,它不会停止。

我尝试在Global.asax中添加<%@ Application Language="C#" Inhertis="Global.asax"%>但我得到的是Could not load type 'Global.asax'

我该怎么办?

编辑:

Global.asax.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
/// <summary>
/// Summary description for Global
/// </summary>
/// 
public class Global
{
    public Global()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = System.Web.Http.RouteParameter.Optional });
    }
}

我创建了一个网站项目来测试这一点(不确定细节,因为我不经常使用网站项目(。我在App_Code文件夹(VS 推荐的位置(中Global.asax.cs类。通过以下设置,我在 Global.asax.cs 中遇到断点:

您的 Global.asax 应该读成这样,假设 Global.asax.cs 中的类名是Global(您可以进行自定义(:

<%@ Application Language="C#" CodeBehind="App_CodeGlobal.asax.cs" Inherits="Global" %>

重要的是,您的类Global必须继承自System.Web.HttpApplication

public class Global : System.Web.HttpApplication
{
    public Global()
    {
    }
    protected void Application_Start() 
    {
        Trace.TraceInformation("In application start");
    } 
}

最新更新