0

Like for example when a user vists the index page he sees content that is different from the content that a logged in user sees.

How can i achieve this, does this require some kind of annotation or something else ?

3 Answers 3

1

Have you tried _Layout view or partial views?

Sign up to request clarification or add additional context in comments.

2 Comments

But how are they related to it ? Shouldn't this routing be the responsibility of controller action ?
If you use mvc authentication you can decide what should be shown in your view,because the User object is access able every where in your view and if you write @User.Identity.IsAuthenticated in a if condition you can decide what to show in view.You can use it in every view.
1

There is more than one way to handle this...

If you want to handle it in the controller or the view its up to you. Ideally I would hazard a guess the choice depends on what you want to display or not display as the case may be.

If you handle it in the controller....

Function Index()As ActionResult
  If Request.IsAuthenticated Then
    ' Logged in is TRUE
    Return View(LoggedInUserContentModel)
  Else
    ' Logged in is FALSE
    Return View(SiteGuestUserContentModel)
  End If
End Function

If you handle it in the View.... DIRECTLY Showing page content

<% If Request.IsAuthenticated Then %>
  <!-- User is Logged In -->
  Welcome <b><%: Page.User.Identity.Name %></b>!
  [ <%: Html.ActionLink("Log Off", "LogOff", "Account")%> ]
<% Else %>
  <!-- User is NOT Logged In -->
  Welcome <b>Guest</b> please login!
  [ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ]
<% End If %>

You may optionally choose not to directly show content embedded on the page itself but may choose to show content retrieved from within a ChildAction (this would be similar to showing it in a UserControl using the old non MVC style of web design as opposed to directly placing the content on the web page)

Comments

1

One simple way of handling it is to use the RedirectToAction in your controller method.

Function Index() As ActionResult
    If User.Identity.IsAuthenticated Then
        Return RedirectToAction("Index", "AuthenticatedController")
    Else
        Return View()
    End If
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.