'context.Request' 引发了 'System.Web.HttpException' 类型的异常
'context.Request' threw an exception of type 'System.Web.HttpException'
我正在尝试在 Global.asax Application_Start() 事件中使用访问 HTTPContext。
var context = HttpContext.Current;
if (context != null)
{
if (context.Request != null) //Getting error here
{
.....
}
}
访问 context.Request
时出现 'context.Request' threw an exception of type 'System.Web.HttpException'
异常。
在这种情况下 context.Request
不是 null 但它抛出异常。
我使用以下代码来识别 Request
属性 是否存在:
context.GetType().GetProperty("Request");
我得到了以下回复。
{System.Web.HttpRequest Request}
Attributes: None
CanRead: true
CanWrite: false
CustomAttributes: Count = 0
DeclaringType: {Name = "HttpContext" FullName = "System.Web.HttpContext"}
GetMethod: {System.Web.HttpRequest get_Request()}
IsSpecialName: false
MemberType: Property
MetadataToken: 385876876
Module: {System.Web.dll}
Name: "Request"
PropertyType: {Name = "HttpRequest" FullName = "System.Web.HttpRequest"}
ReflectedType: {Name = "HttpContext" FullName = "System.Web.HttpContext"}
SetMethod: null
I am not sure how to confirm if context.Request exists and is not null ?
ASP.NET will throw an exception if you try to use this property when the HttpRequest object is not available. For example, this would be true in the Application_Start method of the Global.asax file, or in a method that is called from the Application_Start method. At that time no HTTP request has been created yet.
Application_Start
并不意味着处理特定请求,因此您需要将您正在做的事情转移到不同事件的处理程序中,例如 BeginRequest
.
public class Global : HttpApplication
{
private static HttpRequest initialRequest;
static Global()
{
initialRequest = HttpContext.Current.Request;
}
void Application_Start(object sender, EventArgs e)
{
//access the initial request here
}
您可以在此处使用 Application_Start 事件。静态类型是在其 HTTPContext 中使用请求创建的,允许您存储它并立即在 Application_Start 事件中重用它。
我正在尝试在 Global.asax Application_Start() 事件中使用访问 HTTPContext。
var context = HttpContext.Current;
if (context != null)
{
if (context.Request != null) //Getting error here
{
.....
}
}
访问 context.Request
时出现 'context.Request' threw an exception of type 'System.Web.HttpException'
异常。
在这种情况下 context.Request
不是 null 但它抛出异常。
我使用以下代码来识别 Request
属性 是否存在:
context.GetType().GetProperty("Request");
我得到了以下回复。
{System.Web.HttpRequest Request}
Attributes: None
CanRead: true
CanWrite: false
CustomAttributes: Count = 0
DeclaringType: {Name = "HttpContext" FullName = "System.Web.HttpContext"}
GetMethod: {System.Web.HttpRequest get_Request()}
IsSpecialName: false
MemberType: Property
MetadataToken: 385876876
Module: {System.Web.dll}
Name: "Request"
PropertyType: {Name = "HttpRequest" FullName = "System.Web.HttpRequest"}
ReflectedType: {Name = "HttpContext" FullName = "System.Web.HttpContext"}
SetMethod: null
I am not sure how to confirm if context.Request exists and is not null ?
ASP.NET will throw an exception if you try to use this property when the HttpRequest object is not available. For example, this would be true in the Application_Start method of the Global.asax file, or in a method that is called from the Application_Start method. At that time no HTTP request has been created yet.
Application_Start
并不意味着处理特定请求,因此您需要将您正在做的事情转移到不同事件的处理程序中,例如 BeginRequest
.
public class Global : HttpApplication
{
private static HttpRequest initialRequest;
static Global()
{
initialRequest = HttpContext.Current.Request;
}
void Application_Start(object sender, EventArgs e)
{
//access the initial request here
}
您可以在此处使用 Application_Start 事件。静态类型是在其 HTTPContext 中使用请求创建的,允许您存储它并立即在 Application_Start 事件中重用它。