【发布时间】:2020-09-01 23:34:12
【问题描述】:
在本地运行时,此代码运行良好。但是,当我以发布模式发布文件并将它们放在服务器上的 wwwroot 文件夹中时,应用程序会运行,但 Application_Begin 请求方法永远不会触发。我通过创建一个简单的文本文件对其进行了测试。
编辑:我刚刚在我的 Application_Start() 方法中创建了一个简单的异常,它也不会触发。所以我的 global.asax 没有开火。这是我的 wwwroot 文件夹中的内容
- Areas 文件夹(帮助文件)
- 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>
【问题讨论】:
-
I tested it by creating a simple text file.你彻底搜索过那个文本文件了吗? -
是的,我做到了。这是不行的。应该把它写在程序所在的目录中
-
Path.Combine而不是string.Concat工作吗?
标签: c# asp.net-web-api asp.net-web-api2