如何创建一个"公开];在ASP.NET用户个人资料页面(3.5) [英] How do I create a "public" user profile page in ASP.NET (3.5)

查看:153
本文介绍了如何创建一个"公开];在ASP.NET用户个人资料页面(3.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了ASP.NET用户控件。

I implemented the ASP.NET user controls.

每个用户都可以登录并更新他们的个人数据,例如私人页面上 myData.html

Each user can log in and update their personal data, for example on the private page myData.html

现在,我想他们的个人资料中的一部分,以显示一个公共只读页面上,例如 - userGreg45public.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 implement or modify the user control, to allow each user to have their own unique public page?

(注 - 有一个当一个特定的人在日志例如,myData.html具有相同的名称,但显示不同的数据被命名为每个用户都一样监守他们只能被访问文件的SET根据谁了,这一公开页日志将不得不为每个成员的唯一文件名)

(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)

感谢你。

推荐答案

这听起来像问题是,你希望每个用户都有自己的网址...这样的话?

It sounds like the issue is you want each user to have their own url... Is this the case?

您可以使用URL重写。一个更简单的方法之一是使用 HttpContext.RewritePath 在你的的Global.asax 文件中的的Application_BeginRequest 方法。

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.

编辑:添加品code

我在这里做一些假设......我想提出的假设是:a)用户有一个名字,B)用户有一个姓氏和c)用户有一些独特的标识符,在我的情况下,我使用的是用户id。

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".

要开始,你将不得不对公众的URL格式决定。像这样的东西可以工作:

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

其中1234为用户标识。

Where 1234 is the userId.

接下来,您将需要一个功能来构建这些URL。事情是这样的可能:

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.

现在,在Global.asax文件,添加以下code:

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"]));
    }
}

有很多东西会在这里的。开始时,通过简单地定义这个功能,ASP.Net将其订阅到的 HttpApplication.BeginRequest 事件。

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.

接下来是由它来完成繁重的正则表达式。它基本上任何请求匹配您的用户个人资料页面,并在内部告诉ASP.Net将请求路由到另一页。新的请求将是这个样子:

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

这是由你来实施 /public/userProfile.aspx 页。有许多方法,使事情只读。这可能是直线前进的更换文本框使用标签控制的控制?

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.

这篇关于如何创建一个&QUOT;公开];在ASP.NET用户个人资料页面(3.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆