<h3><%= User.Identity.IsAuthenticated ? true : User.Identity.Name; %></h3>
I’d like to write a one-line code that returns the member’s name when the member is already logged in.
But the IsAuthenticated code already returns true,
so do i need to use true ?
Also, I don’t want to do anything unless user not logged in.
in this case. When you want to write concise code in one line. Which code would be best?
3
Answers
you can use this
<h3>@(User.Identity.IsAuthenticated ? User.Identity.Name :"")</h3>
You can use a conditional operator which has the form:
In your case this would be:
There is a better way that you can do this, By using
Tag helper
which explained in this article, you can rewrite your code like this:Condition tag helper implementation is :