Reputation: 35
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 });
}
}
Upvotes: 3
Views: 4128
Reputation: 6674
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");
}
}
Upvotes: 2