I implemented the ASP.NET user controls.
Each user can log in and update their personal data, for example on the private page myData.html
Now, I would like a part of their profile to be displayed on a public read-only page, for example - userGreg45public.html
How do I implemen开发者_Python百科t or modify the user control, to allow each user to have their own unique public page?
(Note - There are a "set" of files that are named all the same for each user becuase they can only be accessed when a specific person logs in. For example, myData.html has the same name but displays different data based on who logs in. The "public page" will have to have a unique file name for each member)
Thank You.
It sounds like the issue is you want each user to have their own url... Is this the case?
You can use URL Rewriting. One of the more simple approaches is to use the HttpContext.RewritePath in the Application_BeginRequest
method of your Global.asax
file.
EDIT: Added Sample Code
I am making some assumptions here... The assumptions I am making are that a) users have a first name, b) users have a last name and c) users have some unique identifier, in my case I am using a "userId".
To start, you will have to decide on a format for the public urls. Something like this could work:
http://www.yoursite.com/public/firstname-lastname-1234.aspx
Where 1234 is the userId.
Next, you will want a function to build these URLs. Something like this perhaps:
String ToPublicUrl(String firstName, String lastName, int userId)
{
return String.Format("http://www.yoursite.com/public/{0}-{1}-{2}.aspx",
Regex.Replace(firstName, "[^a-zA-Z]", ""),
Regex.Replace(lastName, "[^a-zA-Z]", ""),
userId);
}
Notice how I am stripping out any non-letter characters.
Now, in the Global.asax file, add the following code:
static Regex ProfileRegex = new Regex(@"/public/(?<firstname>[a-zA-Z]+)-(?<lastname>[a-zA-Z]+)-(?<userid>[0-9]+)\.aspx$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
void Application_BeginRequest(object sender, EventArgs e)
{
Match match = ProfileRegex.Match(Context.Request.FilePath);
if( (match != null) && match.Success )
{
Context.RewritePath((String.Format("~/public/userProfile.aspx?userId={0}",
match.Groups["userid"]));
}
}
There is a lot of stuff going on here. To start, by simply defining this function, ASP.Net will subscribe it to the HttpApplication.BeginRequest event.
Next is the Regex which does the heavy lifting. It basically matches any requests to your a user profile page, and internally tells ASP.Net to route the request to another page. The new requests will look something like this:
http://www.yoursite.com/public/userProfile.aspx?userId=1234
It is up to you to implement the /public/userProfile.aspx
page. There are numerous ways to make things readonly. It might be as straight forward as replacing TextBox controls with Label controls?
If you have multiple pages you want to make public, you could implement multiple rewrite rules. (Although at some point you are better off using a component like this or this).
As an FYI, I referenced samples on this page for the rewrite stuff. Let me know if you have any questions.
You can by default assign a role like "guest" to the user who has visited the site.
I just implemented something like this on a site. I created two pages. One that was private and required the user to be logged in. This page allowed the user to edit their profile.
And the other page was a public page. It didn't allow edits, and it was in a location viewable by anyone.
精彩评论