locked
Interpreting a variable in Response.Redirect RRS feed

  • Question

  • User-157557353 posted

    Is there anyway to get a variable interpreted in Response.Redirect?

    For example:

                var ABC = 20;       

                Response.Redirect("~/AddFINSTAT?id=@ABC");

    In my "~/AddFINSTAT” page:

     

                CXXId = Request.QueryString["ID"]; 
                <p>@CXXId</p>

     

    It shows “@ABC” instead of 20. It doesn’t seem to get interpreted.

    Any suggestions? Thanks.

    Wednesday, March 9, 2016 3:02 PM

Answers

  • User-1980594115 posted

    Try the following:

    var ABC = 20;
    var userpage = "~/AddFINSTAT?id=" + ABC;   
    
    Response.Redirect(userpage);
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 3:20 PM
  • User1853794821 posted

    I believe in Razor, the "@" sign is used to toggle between html and code.  I do not believe it works inside a string.,

    A couple or workarounds:

    (1)  Response.Redirect("~/AddFINSTAT?id"+ ABC");

    or

    (2) Response.Redirect(String.Format("~/AddFINSTAT?id={0}",ABC));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 3:44 PM
  • User1124521738 posted

    @ syntax in razor is for handling serverside stuff within html blocks not within literal C# code - you should update

    Response.Redirect("~/AddFINSTAT?id=@ABC");

    to

    Response.Redirect("~/AddFINSTAT?id=" + ABC);

    or

    Response.Redirect(String.Format("~/AddFINSTAT?id={0}",  ABC));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 4:58 PM

All replies

  • User-1980594115 posted

    Try the following:

    var ABC = 20;
    var userpage = "~/AddFINSTAT?id=" + ABC;   
    
    Response.Redirect(userpage);
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 3:20 PM
  • User1853794821 posted

    I believe in Razor, the "@" sign is used to toggle between html and code.  I do not believe it works inside a string.,

    A couple or workarounds:

    (1)  Response.Redirect("~/AddFINSTAT?id"+ ABC");

    or

    (2) Response.Redirect(String.Format("~/AddFINSTAT?id={0}",ABC));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 3:44 PM
  • User1124521738 posted

    @ syntax in razor is for handling serverside stuff within html blocks not within literal C# code - you should update

    Response.Redirect("~/AddFINSTAT?id=@ABC");

    to

    Response.Redirect("~/AddFINSTAT?id=" + ABC);

    or

    Response.Redirect(String.Format("~/AddFINSTAT?id={0}",  ABC));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 9, 2016 4:58 PM
  • User-157557353 posted

    Thanks for the solutions!  They really help!

    Thursday, March 10, 2016 12:49 AM