UrlData is dependent on the folder structure of your site. What you are trying to do will work if Checkout.cshtml is in the root of your site. What happens if you add @ObjectInfo.Print(UrlData) to the page?
string str = Request.ServerVariables["HTTP_HOST"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
//Here you will get http:/localhost:6819/Checkout/17 in str
int position = str.LastIndexOf('/');
if (position > -1)
str = str.Substring(position + 1);
I am sorry I think I made a mistake; I thought it will not make a difference but probably it is.
The situation is - the data is posted to Checkout which gets redirected to login page if user is not logged in, so I loose the posted data. So I added the data with the sender info using html helper and the resulting complete URL was
I thought I will extract the last bit and get redirected back to the checkout page but it did not work.
With mahedee solution posted I am getting 'Login' back, that's when I realize the different nature of two urls. Also if when use html helper @Href I don't get redirected back to checkout page from login page, which I can if I don't use html helper but then
I can add this id data to it.
So the actual solution I am looking for is to how to keep the data when I am redirected to the login page and then back to checkout page? I am surely missing something basic here.
Have a look at the Starter Site template site. You should see that if you add WebSecurity.RequireAuthenticatedUser() to the top of Checkout.cshtml, the user will be redirected to the login page if you try to browse to Checkout/17, and the Url will contain
a querystring:
?ReturnUrl=%2fCheckout%2f17
In Login.cshtml. you see this:
if (WebSecurity.Login(username, password, rememberMe)) {
var returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl.IsEmpty()){
Response.Redirect("~/");
} else {
Context.RedirectLocal(returnUrl);
}
}
The RedirectLocal method will transfer the user to the querystring Url, preserving the UrlData.
SHR
Member
30 Points
65 Posts
Extracting UrlData
May 01, 2012 11:22 PM|LINK
Hi
This is my url http:/localhost:6819/Checkout/17
and would like to extract 17.
Tried
var sesid = UrlData[0].AsInt();
but it is returning 0
tried without AsInt() and also @UrlData[0] but no success
what I am doing wrong? any help please
Thanks
Hamid
2pac
Participant
1586 Points
269 Posts
Re: Extracting UrlData
May 02, 2012 01:18 AM|LINK
To get data from URL you need to use QueryString. for example: lets take a hyperlink control
To extract 17 from the above url:
int getId = Convert.ToInt32(Request.QueryString["Id"]);Hope this helps
Regards,
Jayesh
Mikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Extracting UrlData
May 02, 2012 05:11 AM|LINK
UrlData is dependent on the folder structure of your site. What you are trying to do will work if Checkout.cshtml is in the root of your site. What happens if you add @ObjectInfo.Print(UrlData) to the page?
Web Pages CMS | My Site | Twitter
mahedee
Member
450 Points
116 Posts
Re: Extracting UrlData
May 02, 2012 03:43 PM|LINK
Try with this. You will get your desired result.
string str = Request.ServerVariables["HTTP_HOST"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString(); //Here you will get http:/localhost:6819/Checkout/17 in str int position = str.LastIndexOf('/'); if (position > -1) str = str.Substring(position + 1);Now you will get 17 in str.
Mahedee
Blog: http://mahedee.blogspot.com
SHR
Member
30 Points
65 Posts
Re: Extracting UrlData
May 02, 2012 05:37 PM|LINK
I am sorry I think I made a mistake; I thought it will not make a difference but probably it is.
The situation is - the data is posted to Checkout which gets redirected to login page if user is not logged in, so I loose the posted data. So I added the data with the sender info using html helper and the resulting complete URL was
http://localhost:6819/Account/Login?sender=http:/localhost:6819/Checkout/17
I thought I will extract the last bit and get redirected back to the checkout page but it did not work.
With mahedee solution posted I am getting 'Login' back, that's when I realize the different nature of two urls. Also if when use html helper @Href I don't get redirected back to checkout page from login page, which I can if I don't use html helper but then I can add this id data to it.
So the actual solution I am looking for is to how to keep the data when I am redirected to the login page and then back to checkout page? I am surely missing something basic here.
Thanks for help.
Hamid
Mikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Extracting UrlData
May 02, 2012 06:08 PM|LINK
Have a look at the Starter Site template site. You should see that if you add WebSecurity.RequireAuthenticatedUser() to the top of Checkout.cshtml, the user will be redirected to the login page if you try to browse to Checkout/17, and the Url will contain a querystring:
In Login.cshtml. you see this:
if (WebSecurity.Login(username, password, rememberMe)) { var returnUrl = Request.QueryString["ReturnUrl"]; if (returnUrl.IsEmpty()){ Response.Redirect("~/"); } else { Context.RedirectLocal(returnUrl); } }The RedirectLocal method will transfer the user to the querystring Url, preserving the UrlData.
Web Pages CMS | My Site | Twitter
SHR
Member
30 Points
65 Posts
Re: Extracting UrlData
May 02, 2012 07:40 PM|LINK
Thank you Mike but I dont get the data back to checkout page after I am redirected to login page
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: Extracting UrlData
May 09, 2012 10:24 AM|LINK
HI
You can refer to this link:
http://forums.asp.net/t/1097333.aspx/1
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Mikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Extracting UrlData
May 09, 2012 10:55 AM|LINK
You need to show the code you are using.
Web Pages CMS | My Site | Twitter
SHR
Member
30 Points
65 Posts
Re: Extracting UrlData
May 10, 2012 08:31 PM|LINK
Thanks Mike
It has been sorted, the error was in passing the id with the ReturnUrl.
Cheers