Application_Begin 发布时请求根本不会触发
Application_Begin Request not firing at all when published
在本地 运行 时,这段代码运行良好。但是,当我以发布模式发布文件并将它们放在服务器上的 wwwroot 文件夹中时,应用程序运行但 Application_Begin 请求方法永远不会触发。我通过创建一个简单的文本文件对其进行了测试。
编辑: 我刚刚在 Application_Start() 方法中创建了一个简单的异常,但它也没有触发。所以我的 global.asax 没有开火。这是我的 wwwroot 文件夹中的内容
- 区域文件夹(帮助文件)
- Bin 文件夹(所有 dll 和项目文件)
- Global.asax
- packages.config (nuget)
- web.config
这里是 application_start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
var config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
CorsConfig.RegisterCors(GlobalConfiguration.Configuration);
var location = HttpContext.Current.Server.MapPath("~");
var file = string.Concat(location, "/data.txt");
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Begin Request");
tw.Close();
}
throw new Exception();
}
我不确定它是一个 dll 还是我缺少的东西。我正在使用 .Net 4.7.2
protected void Application_BeginRequest()
{
var location = HttpContext.Current.Server.MapPath("~");
var file = string.Concat(location, "/data.txt");
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Begin Request");
tw.Close();
}
if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OrdinalIgnoreCase) &&
Request.HttpMethod == "OPTIONS")
{
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Options Hit");
tw.Close();
}
Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session, Authorization, USER_NBKID, Role, access-control-allow-credentials");
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
Response.End();
}
}
我的 webconfig 也有这个部分,但它仍然不起作用
<system.web>
<caching>
<outputCache enableOutputCache="false" />
</caching>
<compilation debug="true" targetFramework="4.7.2" />
<customErrors mode="Off" />
<authentication mode="None" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
**<modules runAllManagedModulesForAllRequests="true">** <---------------------
<remove name="FormsAuthentication" />
<remove name="WebDAVModule" />
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="https://www.myaspwebsite.com" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD" />
<add name="Access-Control-Allow-Headers" value="access-control-allow-origin, access-control-allow-credentials, Origin, X-Requested-With, Content-Type, Accept,Authorization,USER_ID,UserRole" />
<add name="Cache-Control" value="no-cache" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
<rewrite>
<outboundRules>
<clear />
<rule name="AddCrossDomainHeader">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_ORIGIN}" pattern="(https?:\/\/((.+\.)?[a-zA-Z0-9-]*\.ap\.dk|(.+\.)?localhost(\:[0-9]*)?))" />
</conditions>
<action type="Rewrite" value="{C:0}" />
</rule>
</outboundRules>
</rewrite>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我发现它没有触发,因为 web.config 中的这一行不准确
<compilation debug="true" targetFramework="4.7.2" />
服务器上没有安装 4.7.2,所以我将其更改为
<compilation debug="true" targetFramework="4.5" />
及其工作原理。搞清楚这么简单的问题花了3天时间
在本地 运行 时,这段代码运行良好。但是,当我以发布模式发布文件并将它们放在服务器上的 wwwroot 文件夹中时,应用程序运行但 Application_Begin 请求方法永远不会触发。我通过创建一个简单的文本文件对其进行了测试。
编辑: 我刚刚在 Application_Start() 方法中创建了一个简单的异常,但它也没有触发。所以我的 global.asax 没有开火。这是我的 wwwroot 文件夹中的内容
- 区域文件夹(帮助文件)
- Bin 文件夹(所有 dll 和项目文件)
- Global.asax
- packages.config (nuget)
- web.config
这里是 application_start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
var config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
CorsConfig.RegisterCors(GlobalConfiguration.Configuration);
var location = HttpContext.Current.Server.MapPath("~");
var file = string.Concat(location, "/data.txt");
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Begin Request");
tw.Close();
}
throw new Exception();
}
我不确定它是一个 dll 还是我缺少的东西。我正在使用 .Net 4.7.2
protected void Application_BeginRequest()
{
var location = HttpContext.Current.Server.MapPath("~");
var file = string.Concat(location, "/data.txt");
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Begin Request");
tw.Close();
}
if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OrdinalIgnoreCase) &&
Request.HttpMethod == "OPTIONS")
{
using (var tw = new StreamWriter(file, true))
{
tw.WriteLine("Options Hit");
tw.Close();
}
Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session, Authorization, USER_NBKID, Role, access-control-allow-credentials");
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
Response.End();
}
}
我的 webconfig 也有这个部分,但它仍然不起作用
<system.web>
<caching>
<outputCache enableOutputCache="false" />
</caching>
<compilation debug="true" targetFramework="4.7.2" />
<customErrors mode="Off" />
<authentication mode="None" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
**<modules runAllManagedModulesForAllRequests="true">** <---------------------
<remove name="FormsAuthentication" />
<remove name="WebDAVModule" />
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="https://www.myaspwebsite.com" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD" />
<add name="Access-Control-Allow-Headers" value="access-control-allow-origin, access-control-allow-credentials, Origin, X-Requested-With, Content-Type, Accept,Authorization,USER_ID,UserRole" />
<add name="Cache-Control" value="no-cache" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
<rewrite>
<outboundRules>
<clear />
<rule name="AddCrossDomainHeader">
<match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_ORIGIN}" pattern="(https?:\/\/((.+\.)?[a-zA-Z0-9-]*\.ap\.dk|(.+\.)?localhost(\:[0-9]*)?))" />
</conditions>
<action type="Rewrite" value="{C:0}" />
</rule>
</outboundRules>
</rewrite>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我发现它没有触发,因为 web.config 中的这一行不准确
<compilation debug="true" targetFramework="4.7.2" />
服务器上没有安装 4.7.2,所以我将其更改为
<compilation debug="true" targetFramework="4.5" />
及其工作原理。搞清楚这么简单的问题花了3天时间