Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
http://localhost:1163/NewProject/subject/test-subject-1.aspx

from this url how can i get the id 1(in the example) using request.QuryString.
Posted

from this url how can i get the id 1(in the example) using request.QuryString.
You can't.
Query string is something that follows the page after ?
Your URL doesn't use query string.

You can use Request.Url.AbsoluteUri instead.

[VB]
Dim s as String = System.IO.Path.GetFileName(Request.Url.AbsoluteUri).ToLower().Replace(".aspx", "")
Dim number as String = s.Substring(s.LastIndexOf("-") + 1)


[C#]
C#
string s = System.IO.Path.GetFileName(Request.Url.AbsoluteUri).ToLower().Replace(".aspx", "");
string number = s.Substring(s.LastIndexOf("-") + 1); 


You can convert it to integer later.

If you really use query string then Request.QueryString["ID"] is enough.
 
Share this answer
 
v8
Comments
rahul dev123 26-May-11 7:15am    
please send details how to store the id in a variable
Prerak Patel 26-May-11 7:20am    
Updated answer accordingly.
rahul dev123 26-May-11 7:32am    
if the test-subject name change for id 2 example test-physics-2 then how can i get this
Prerak Patel 26-May-11 7:36am    
Then try regex (?:[a-z\-]+)(\d)+(?:\.aspx)
rahul dev123 26-May-11 7:42am    
how to use regex please send me details and also previous code not working
You can't use Request.QueryString because you don't have a query string. In order to do what is is that you appear to want to do, your example URL needs to look like this:

http://localhost:1163/NewProject/subject/test-subject-1.aspx?ID=1
At that point, you can do this:

C#
int id;
Int32.TryParse(Request.QueryString["ID"], out id);


Without knowing more about your specific environment and requirements, I can't get any more specific than that, and I'm assuming that you have the programming chops to know how to ensure that your code will handle all possible contingencies regarding validty of data.
 
Share this answer
 
Comments
rahul dev123 26-May-11 8:05am    
I already done this procedure......but i can't use like this
hai,

if you want to get request.QuryString. you have to give QuryString.

here is the ex: will help you



MIDL
//on navigavigation gilike this aspx?ID=1 here the id the value
    http://localhost:1163/NewProject/subject/test-subject-1.aspx?ID=1
    //when you want to get it
    if(Request.QueryString["ID"] != null)
{
    string id=Request.QueryString["Request"].ToString();
}
 
Share this answer
 
Comments
#realJSOP 26-May-11 7:44am    
Look at his question again - he doesn't have a query string - he just has a URL.
anvas kuttan 26-May-11 7:51am    
ya that's why i put a query string first... ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900