nemostyle
nemostyle

Reputation: 814

Is there any other way to use Session variable in cshtml?

I just readed answers from this question Use Session variable in html, but this is not working for me. I tried = instead of : but same thing. I tried this also:

Hello <%Response.Write(Session("username"))%>

but nothing happens. I am using MVC pattern in ASP.NET application. I want to have format:

"Hello Username"

shown on the screen but instead of that I have

"Hello <%:Session["username"]%>".

This is my code in .cs file:

Session["username"] = user.Username;
return RedirectToAction("Index", "Home");

This is my code in .cshtml file:

@if (Session["username"] != null)
{
    <li>
        Hello <%:Session["username"]%> 
    </li>
}

Any other idea?

Upvotes: 0

Views: 2759

Answers (1)

Steve
Steve

Reputation: 11963

The <% syntax is for old asp.

For razor, you can simply do @Session["username"].

Upvotes: 1

Related Questions