I wish to use C# and Razor syntax to check if a cookie has been set. If it has been set, I want to show
<h2> Cookie set </h2>.
If it hasn't, I want to display
<h2>Cookie not set</h2>
So, to review a few things, I have this setting the cookie :
//set cookie
HttpCookie cookie = Request.Cookies.Get("stackOverflowCookie");
if(cookie == null) {
cookie = new HttpCookie("stackOverflowCookie");
cookie.Value = "Hi guys!";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
Using Razor, what is the best way, syntactically, to render what I wish? Whatever I try results in compilation errors :
@{
if(Request.Cookies["stackOverflowCookie"] == null){
//some other logic is here in my actual code, so an inline statement is not sufficient
<h2> Cookie set </h2>
@}
@{ else {
<h2> Cookie not set </h2>
@}
Obviously this is horrible looking, and it doesn't work. It does show the functionality I would like those. How is the best way to achieve this functionality?