473,656 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cur rent.Request.Pa th

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

Match m=Regex.Match(R equest.Url.Abso lutePath, "[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.Cur rent.Request.Pa th

}
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.Cur rent.Request.Pa th

}

Are these the only possible urls?

Then you could use a regex:
(?<=somedirecto ry/)\d+(?=(?:/somepage)?\.asp x)

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.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numer icPart);
}
return -1;
}
Jesse Houwing
Aug 3 '06 #3
I'm using this in a class file.

Match m=Regex.Match(R equest.Url.Abso lutePath, "[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(R equest.Url.Abso lutePath, "[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.Cur rent.Request.Pa th

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

What about this:

if (HttpContext.Cu rrent.Request.P ath.Length 0)
return
System.Text.Reg ularExpressions .RegEx.Replace( HttpContext.Cur rent.Request.Pa th, "[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.Cur rent.Request.Pa th

}


Are these the only possible urls?

Then you could use a regex:
(?<=somedirecto ry/)\d+(?=(?:/somepage)?\.asp x)

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.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numer icPart);
}
return -1;
}
Jesse Houwing
Aug 3 '06 #5
I want to return the number as a string still, not an integer:

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

public string GetRewrittenURL (string URL) {

GetFormattedRec ipeDetailURL(UR L);
....

}

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.Cur rent.Request.Pa th

}


Are these the only possible urls?

Then you could use a regex:
(?<=somedirecto ry/)\d+(?=(?:/somepage)?\.asp x)

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.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numer icPart);
}
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.Cu rrent.Request.P ath.Length 0)
return
System.Text.Reg ularExpressions .RegEx.Replace( HttpContext.Cur rent.Request.Pa th, "[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.Cur rent.Request.Pa th

}

Are these the only possible urls?

Then you could use a regex:
(?<=somedirect ory/)\d+(?=(?:/somepage)?\.asp x)

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.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h, @"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
string numericPart = m.Value;
return int.Parse(numer icPart);
}
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.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h,
@"(?<=\w+/)\d+(?=(?:/\w+)?\.aspx)");
if (m.Success)
{
return m.Value;
}
return stri;

public string GetRewrittenURL (string URL) {

GetFormattedRec ipeDetailURL(UR L);
...

}

Error:
Try this:

string path = HttpContext.Cur rent.Request.Pa th;
Match m = Regex.Match(pat h, @"(?<=\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.co m

"dba123" <db****@discuss ions.microsoft. comwrote in message
news:8F******** *************** ***********@mic rosoft.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.Cur rent.Request.Pa th

}

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
4520
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 this time? This is the code the retrieves the values: if (($hasRegistered || $hasPreRegistered) && !empty($uplinenumber)) { // CHECK TO SEE IF UPLINE NUMBER IS A VALID NUMBER $regNumberGenerator = new RegNumberGenerator($uplinenumber,...
20
2334
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
4137
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 additon operator (which returns another Point object, and which my question is about) are as follows: Point::Point(int x, int y) : _x(x), _y(y) { }
2
2342
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 line is very long. I'm wondering if there is any way to suppress it. I can only think of typedef. But I'm not sure whether I can use typedef for the return type. Would you please help me? Please don't be daunted by the length of the code.
2
4661
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 Center: To return a result set from a procedure to the originating application, use the WITH RETURN TO CLIENT clause. When WITH RETURN TO CLIENT is specified on a result set, no nested procedures can access the result set.
15
6723
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 i = 0,j; j = sample(0);
10
19134
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 ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using System; using System.Runtime.InteropServices; //
12
3783
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. 1. The Deserialization goes like: #Region " Load "
3
4547
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
6
2403
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 Card { // instance variables //suits private int suit; private int spades;
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.