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?