我有网站项目并且我已经开始开发 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
做同样的事情时, 它不会停止。
我试过添加 <%@ Application Language="C#" Inhertis="Global.asax"%>
里面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.cs
中的类名是 Global
(您可以自定义),您的 Global.asax 应该是这样的:
<%@ Application Language="C#" CodeBehind="App_Code\Global.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");
}
}
关于c# - 手动创建的 global.asax.cs 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433151/