简体   繁体   中英

Response.Redirect passing a value dynamically

net c#.

I have a page A with this code

//ON PAGE A:        
string contentId = ContentIdFromUrl;
Response.Redirect("~/x/y/b.aspx?ContentId={0}"contentId);

and I need redirect the user to page B passing in the URL a variable in my case ContentId .

I receive a syntax error with my code.

Could you write a correct version?

Thanks for your time.

string contentId = ContentIdFromUrl;
Response.Redirect("~/x/y/b.aspx?ContentId="+HttpUtility.UrlEncode(contentId));

You'll have to change the last line to:

Response.Redirect(string.Format("~/x/y/b.aspx?ContentId={0}",contentId));

as it won't compile as it is.

Update: Another way to do this is:

Response.Redirect("~/x/y/b.aspx?ContentId=" + contentId);
string contentId = ContentIdFromUrl;
Response.Redirect(string.Format("~/x/y/b.aspx?ContentId={0}",contentId)); 
Response.Redirect(string.Format("~/x/y/b.aspx?ContentId={0}",contentId));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM