Search code examples
asp.netasp.net-mvcuser-controlsmarkup

ASP.Net MVC "logged in" best practice in view/markup


Quick question. If I wanted to say, have a login button if user isn't logged in, and some other markup if the user is logged in, what would be the best approach?

I'm reluctant to go with the

<% if(user.loggedIn){ %>
    <!-- do something -->
<% }else{ %>
    <!-- do something else -->
<% } %>

approach, since I hate code in my markup. Would I be way off base, if I thought a 'pretty' way to do this was to make a user control to accomplish this? and then have the code in the user control code-behind? Can anyone give me a quick 'n dirty example?


Solution

  • I'm reluctant to go with the ...

    right approach.

    and then have the code in the user control code-behind

    No, it wouldn't be any better than simple logic in the view. In fact, you can't avoid logic in the view. For presentation purposes, it's just fine. Just watch out not to go too far.