开发者

Displaying same content to different users who may be seeing different master pages (ASP.net)

I have some pages that have content that is relevant to both logged in users and non logged in users. For example, pages with contact information, privacy policies, etc. All the pages have your typical navigation menu but the thing is logged in users normally see a different navigation menu bar than non logged in users.

What is the best way to do this in ASP.net?

So far, possible solutions include the following:

  1. Displaying the content using a pop up window. The page will contain no menu and is just some basic page doesn't need to check what type of user is seeing it.
  2. Programmatically changing the master page depending on whether the user is authenticated or not. However, there are some variables on one of the master pages that need to be accessed but isn't touched at all by non logged in users.
  3. Putting the content in a user control and sticking this user control on two separate pages to be displayed to the appropriate user.

I'm not really a fan of #1 because users visiting the site for the first time may have some type of popup blocker or have javascript disabled.

I know #2 is possible by having the page use some type of base class that has inherited from MasterPage. However, I've read that this might not be the best design since now one of the pages has access to variables that isn't really necessary.

The third method sounds reasonable but then there'd be two separate ASPX files.

Is there a proper way of doing this? Or another method I haven't thought of yet?

edit

To clarify, logged in users need to set certain variables in their master pages where non logged in users do not. The reason for this is that there is a user control that displays a special navigation menu that will开发者_运维百科 highlight certain items depending on these variables.

For example, the user control requires a string to determine which item to highlight. A page with profile information will provide "profile" as a parameter that will highlight the "Profile" item on the menu.

The menu in the user control is generated dynamically based on data from the database. The menu items are grouped by category and are displayed with an appropriate heading that is also pulled from the DB.


Programmably changing the master page is easy; just supply the correct URL on pre init, set

protected override void OnPreInit(..)
{
    if (this.User != null) {
       if (this.User.Identity.IsAuthenticated)
            this.MasterPageFile = "~/loggedin.master";
       else
            this.MasterPageFile = "~/notloggedin.master";
    }
}

No base class needed for this.

User control approach would work too, but changing master page file is really easy to do.

EDIT: If you have properties to set or get from the master, you could have the code-behind file implement the interface, and check if the this.Master reference is of that interface type.

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜