开发者

Response.Redirect passing a value dynamically

开发者 https://www.devze.com 2023-03-16 03:07 出处:网络
net c#. I have a page A with this code //ON PAGE A: string contentId = ContentIdFromUrl; Response.Redirect(\"~/x/y/b.aspx?ContentId={0}\"content开发者_JAVA技巧Id);

net c#.

I have a page A with this code

//ON PAGE A:        
string contentId = ContentIdFromUrl;
Response.Redirect("~/x/y/b.aspx?ContentId={0}"content开发者_JAVA技巧Id);

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消