检查并从当前页面的asp.net的url获取参数

时间:2022-06-11 10:24:03

am trying to get the parameter "id" from the query string of the requested page like this

我试图从所请求页面的查询字符串中获取参数“id”,如下所示

If Request.QueryString IsNot Nothing AndAlso _
            Request.QueryString.GetKey("id") IsNot Nothing Then
            DeleteVehicle(Request.QueryString.GetKey("id"))
 End If

but i get this error

但是我得到了这个错误

System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 16:         If Request.QueryString IsNot Nothing AndAlso _
Line 17:             Request.QueryString.GetKey("id") IsNot Nothing Then

Source File: G:\projects_backup\Toaab\Toaa\admin\vehicle\view.aspx.vb    Line: 16 

please can you help me

拜托,你能帮我吗?

EDIT

Am calling this in the page_load event

我在page_load事件中调用它

the same page (which has autogenerated link) is called through a hyperlink

通过超链接调用相同的页面(具有自动生成的链接)

i also change the code to

我也将代码更改为

If Request.QueryString("id") IsNot Nothing OrElse Request.QueryString("id") IsNot String.Empty Then
 DeleteVehicle(Request.QueryString("id").ToString)
End If

2 个解决方案

#1


4  

To see if a value exists in the QueryString, check to see whether the value equates to an empty string rather than null:

要查看QueryString中是否存在值,请检查该值是否等于空字符串而不是null:

Try this instead:

试试这个:

If String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id"))
End If

#2


1  

  1. There is no Overload in C#/VB.Net which provides String Parameter in GetKey function.
  2. C#/ VB.Net中没有Overload,它在GetKey函数中提供String参数。

  3. If you check all event of Page Life Cycle, Request.QueryString value never comes null(Besides, it contains some not null value).
  4. 如果检查Page Life Cycle的所有事件,则Request.QueryString值永远不会为null(此外,它包含一些非null值)。

  5. Try Changing your code like below.
  6. 尝试更改您的代码,如下所示。


If Request.QueryString("id") IsNot Nothing 
                 AndAlso String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id").ToString)
End If

#1


4  

To see if a value exists in the QueryString, check to see whether the value equates to an empty string rather than null:

要查看QueryString中是否存在值,请检查该值是否等于空字符串而不是null:

Try this instead:

试试这个:

If String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id"))
End If

#2


1  

  1. There is no Overload in C#/VB.Net which provides String Parameter in GetKey function.
  2. C#/ VB.Net中没有Overload,它在GetKey函数中提供String参数。

  3. If you check all event of Page Life Cycle, Request.QueryString value never comes null(Besides, it contains some not null value).
  4. 如果检查Page Life Cycle的所有事件,则Request.QueryString值永远不会为null(此外,它包含一些非null值)。

  5. Try Changing your code like below.
  6. 尝试更改您的代码,如下所示。


If Request.QueryString("id") IsNot Nothing 
                 AndAlso String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id").ToString)
End If