473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Return ID from URL

what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}
Aug 3 '06 #1
9 2360
This is very simplistic, untested code, but you should get the idea:

Match m=Regex.Match(Request.Url.AbsolutePath, "[0-9]");
if (m.Length 0)
Response.Write("found" + m.Value);

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dba123" wrote:
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}
Aug 3 '06 #2
* dba123 wrote, On 3-8-2006 22:03:
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}

Are these the only possible urls?

Then you could use a regex:
(?<=somedirectory/)\d+(?=(?:/somepage)?\.aspx)

Or if the somepage/somedirectory part is random:
(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)

// Check requested URL for ID value, if so return it
public String GetRecipeURLID()
{
string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numericPart);
}
return -1;
}
Jesse Houwing
Aug 3 '06 #3
I'm using this in a class file.

Match m=Regex.Match(Request.Url.AbsolutePath, "[0-9]");

Error The name 'Request' does not exist in the current context

dba123
"Peter Bromberg [C# MVP]" wrote:
This is very simplistic, untested code, but you should get the idea:

Match m=Regex.Match(Request.Url.AbsolutePath, "[0-9]");
if (m.Length 0)
Response.Write("found" + m.Value);

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dba123" wrote:
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}
Aug 3 '06 #4
Thanks Jesse, will try that!

What about this:

if (HttpContext.Current.Request.Path.Length 0)
return
System.Text.RegularExpressions.RegEx.Replace(HttpC ontext.Current.Request.Path, "[0-9]", "");
else
return -1;

what does the -1 tell you in the return to the calling method and what does
it do, stop the calling method if -1 is returned from the called method?--
dba123
"Jesse Houwing" wrote:
* dba123 wrote, On 3-8-2006 22:03:
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}


Are these the only possible urls?

Then you could use a regex:
(?<=somedirectory/)\d+(?=(?:/somepage)?\.aspx)

Or if the somepage/somedirectory part is random:
(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)

// Check requested URL for ID value, if so return it
public String GetRecipeURLID()
{
string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numericPart);
}
return -1;
}
Jesse Houwing
Aug 3 '06 #5
I want to return the number as a string still, not an integer:

string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path,
@"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
return m.Value;
}
return -1;

public string GetRewrittenURL(string URL) {

GetFormattedRecipeDetailURL(URL);
....

}

Error:
--
dba123
"Jesse Houwing" wrote:
* dba123 wrote, On 3-8-2006 22:03:
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}


Are these the only possible urls?

Then you could use a regex:
(?<=somedirectory/)\d+(?=(?:/somepage)?\.aspx)

Or if the somepage/somedirectory part is random:
(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)

// Check requested URL for ID value, if so return it
public String GetRecipeURLID()
{
string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numericPart);
}
return -1;
}
Jesse Houwing
Aug 3 '06 #6
* dba123 wrote, On 3-8-2006 23:32:
Thanks Jesse, will try that!

What about this:

if (HttpContext.Current.Request.Path.Length 0)
return
System.Text.RegularExpressions.RegEx.Replace(HttpC ontext.Current.Request.Path, "[0-9]", "");
This will strip all the numbers from the url, that is now what you wanted.
else
return -1;

what does the -1 tell you in the return to the calling method and what does
it do, stop the calling method if -1 is returned from the called method?--
dba123
You'll need a way to communicate if a number was found in the string. I
choose -1, as it is an unlikely number and can easily tested against.

Jesse
>

"Jesse Houwing" wrote:
>* dba123 wrote, On 3-8-2006 22:03:
>>what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}

Are these the only possible urls?

Then you could use a regex:
(?<=somedirectory/)\d+(?=(?:/somepage)?\.aspx)

Or if the somepage/somedirectory part is random:
(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)

// Check requested URL for ID value, if so return it
public String GetRecipeURLID()
{
string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numericPart);
}
return -1;
}
Jesse Houwing
Aug 4 '06 #7
* dba123 wrote, On 3-8-2006 23:36:
I want to return the number as a string still, not an integer:

string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path,
@"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
return m.Value;
}
return stri;

public string GetRewrittenURL(string URL) {

GetFormattedRecipeDetailURL(URL);
...

}

Error:
Try this:

string path = HttpContext.Current.Request.Path;
Match m = Regex.Match(path, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
return m.Value;
}
else
{
return string.empty;
}
It returns either the number found in the url, or string empty.

Note that the regex I've written is quite strict and should only find
numbers in urls that are similar to the formats you had sent. Please
make sure it does what you need by testing a few samples.

Jesse
Aug 4 '06 #8
With all due respect to the other posters here, using Regex is overkill
for this.

I would recommend passing the URL to the Split method on the string
class, passing the "/" character to split the directories. Then, all you
have to do is check the appropriate element for the id (assuming it is in
the same place in the URL every time). Then, you can pass the string to the
static Parse method on Int32 to get an integer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dba123" <db****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
what's the best way to check the incoming URL for an IDand return the ID
possible examples of incoming URLs:
/somedirectory/4/somepage.aspx
/somedirectory/4.aspx
// Check requested URL for ID value, if so return it
public String GetRecipeURLID(string URL)
{
//somehow check for a number here and return the number
HttpContext.Current.Request.Path

}

Aug 4 '06 #9
* Nicholas Paldino [.NET/C# MVP] wrote, On 4-8-2006 5:45:
With all due respect to the other posters here, using Regex is overkill
for this.

I would recommend passing the URL to the Split method on the string
class, passing the "/" character to split the directories. Then, all you
have to do is check the appropriate element for the id (assuming it is in
the same place in the URL every time). Then, you can pass the string to the
static Parse method on Int32 to get an integer.

Hope this helps.

From the origina post I read that there are multiple formats, some of
which don't have the number as a directory, some of which have. In order
to support all these formats you'll have to do a lot of splits and checks.

That's why I preferred the regex.

Jesse
Aug 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.