3

I have Web Site Project and I have started developing api (atleast trying).

Problem is with creating Global.asax file. Since this is Web Site Project, when I create Global Application Class it creates only Global.asax without Global.asax.cs

After some research, I have found out I need to do it manually so I created Global.asax.cs file and it automatically set it under my already created Global.asax which I think is good.

Problem is I think that my code inside Global.asax.cs isn't running. I tried putting breakpoint inside Global.asax - Application_Start function and it stops there (which mean it is working), but when I do the same at Global.asax.cs, it doesn't stop.

I have tried adding <%@ Application Language="C#" Inhertis="Global.asax"%> inside Global.asax but what I get is Could not load type 'Global.asax'

What should I do?

EDIT:

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 });
    }
}
Aleksa Ristic
  • 35
  • 1
  • 7

1 Answers1

2

I created a web site project to test this out (wasn't sure on details since I don't work with web site projects much). I have Global.asax.cs class within an App_Code folder (location recommended by VS). With the following setup, I'm hitting breakpoints inside Global.asax.cs:

Your Global.asax should read something like this, assuming that the class name in Global.asax.cs is Global (you could make that custom):

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

Importantly, your class Global must inherit from System.Web.HttpApplication:

public class Global : System.Web.HttpApplication
{
    public Global()
    {
    }

    protected void Application_Start() 
    {
        Trace.TraceInformation("In application start");
    } 
}
p e p
  • 6,593
  • 2
  • 23
  • 32
  • It is Web Site Project, it doesn't have namespace or am I wrong? – Aleksa Ristic Jan 24 '18 at 23:14
  • Tbh, I'm not sure. You may not need the namespace - but the point is, `Inherits` should definitely not include `.asax`. Does `Inherits="Global"` work? – p e p Jan 24 '18 at 23:19
  • `<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global"%>` I have this and it says `Could not load type 'Global'` – Aleksa Ristic Jan 24 '18 at 23:25
  • Inside `Global.asax.cs`, what is the class name that inherits from System.Web.HttpApplication? – p e p Jan 24 '18 at 23:28
  • See edits, I created a web site project & am able to hit global.asax.cs breakpoints. Obviously if your global.asax.cs is not within the `App_Code` folder, exclude that from the `Codebehind` path. This is with a manually-created `Global.asax.cs`. – p e p Jan 24 '18 at 23:47
  • In my Global class I do not have `: System.Web.HttpApplication` and for some reason I cannot add it. – Aleksa Ristic Jan 24 '18 at 23:59