如何创建“公众"?ASP.NET (3.5) 中的用户配置文件页面 [英] How do I create a "public" user profile page in ASP.NET (3.5)

查看:21
本文介绍了如何创建“公众"?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 具有相同的名称,但显示的数据不同基于谁登录.公共页面"必须为每个成员有一个唯一的文件名)

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

谢谢.

推荐答案

听起来问题是您希望每个用户都有自己的 url... 是这样吗?

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

您可以使用 URL 重写.一种更简单的方法是使用 HttpContext.RewritePathGlobal.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.

添加示例代码

我在这里做一些假设...我做的假设是 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".

首先,您必须确定公共网址的格式.像这样的东西可以工作:

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 是用户 ID.

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 文件中,添加以下代码:

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 页面.有很多方法可以使事物只读.它可能就像替换 TextBox 带有 Label 控件的控件?

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?

如果您要公开多个页面,则可以实施多个重写规则.(虽然在某些时候你最好使用 thisthis).

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.

这篇关于如何创建“公众"?ASP.NET (3.5) 中的用户配置文件页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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