views:

320

answers:

3

In a view in ASP.NET MVC I can access the profile and profile members like this:

<%= Profile.Name %> - <%= Profile.Karma %>

but how do I get the Profile variable populated? It seems to be in HttpContext.Current.Profile. I've wrote a custom Profile class with a CurrentUser method that looks like this:

static public Profile CurrentUser {
    get {
        return (Profile) (ProfileBase.Create(Membership.GetUser().UserName));
    }
}

I have that class pointed to in my Web.config:

<profile inherits="MyProject.Models.Profile" defaultProvider="AspNetSqlProfileProvider" enabled="true">
    <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
</profile>

This:

var p = CurrentUser.Profile

successfully gives me the profile, but I cannot set it like this:

HttpContext.Profile = CurrentUser.Profile;

because HttpContext.Profile is read only. I get the error:

Error 18 Property or indexer 'System.Web.HttpContextBase.Profile' cannot be assigned to -- it is read only C:...\Controller.cs 26 17 MyProject

Also, I need the Profile to be assigned for every view, so that the name of the logged in user can be displayed on the top right in the usual way. How should I do it?

A: 

I need the Profile to be assigned for every view, so that the name of the logged in user can be displayed on the top right in the usual way

You can create a log on control, and in the control, write this.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
    }
    else {
%> 
        [ <%= Html.ActionLink("Log On", "LogOn", "Account") %> ]
<%
    }
%>

HttpContext object has an user property as well.

J.W.
You are never accessing the profile there.
J. Pablo Fernández
I may misunderstand your purpose here, since you want "the name of the logged in user can be displayed on the top right in the usual way", I just offer my way of doing this.
J.W.
But that's the username, not the name.
J. Pablo Fernández
I see, but even though HttpContext.Profile is read only, you can still still assign values to its members, HttpContext.Profile = CurrentUser.Profile is not working, but you can do something like thisHttpContext.Profile["Name"] = name; And then in your view, you can do ViewData["memberName"] = HttpContext.Profile["Name"] to display data.
J.W.
A: 

Presumably you have set the default profile provider in web.config

<profile defaultProvider="MyCustomProfileProvider">
  <properties>
    <!-- etc. -->
  </properties>
</profile>

Not sure about MVC, but for standard asp.net that is required.

Dan Diplo
Yes, I have that. I'll add it to the question.
J. Pablo Fernández
A: 

The reason why I was getting an empty name was not that I wasn't getting the profile, but that the profile in the database was effectively empty (due to an unrelated bug in my code). Everything I talked about was all that was needed to get the profile working.

J. Pablo Fernández