0

I am new to the asp.net c# development i need to get "test.aspx" part only from this string "/test1/Pages/Request/test.aspx?id=-1" .Please help me if some one know better way to do that ,i check some regular expression methods but still i haven't correct solution.

Thanks

3
  • do you need it IN the test.aspx page by chance? in other words, do you want the name of the current requested page? Commented Sep 15, 2014 at 8:12
  • yes ,i need to get current request page name,i create base page and inherit other pages from base page and get request URL actual requirement is i want to display request page name only . Commented Sep 15, 2014 at 8:25
  • the just get the current page name: stackoverflow.com/a/1833313/72746 Commented Sep 15, 2014 at 8:29

4 Answers 4

1

Have a look at Uri.AbsolutePath

So can do

Path.GetFileName(Request.Url.AbsolutePath)
Sign up to request clarification or add additional context in comments.

Comments

0
string str = "/test1/Pages/Request/test.aspx?id=-1";
string page = str.Substring(str.LastIndexOf('/')+1, str.IndexOf('?')-str.LastIndexOf('/')-1);

1 Comment

Do you need to do this?
0
([a-zA-Z.]+)(?=\?)

You can use this.Just grab the first group.See demo.

http://regex101.com/r/jT3pG3/2

Comments

0

Assuming your pages will all be .aspx:
(?<=/)\w+.aspx(?=\?)

In C# use:
@"(?<=/)\w+.aspx(?=\?)"

---Edit---
If you want to catch more characters, you can modify the bold part of the expression below:
(?<=/)[\w\.]+.aspx(?=\?)

The second regex will also match dots in the part preceding .aspx.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.