简体   繁体   English

如何在ASP.NET(3.5)中创建“公共”用户配置文件页面

[英]How do I create a “public” user profile page in ASP.NET (3.5)

I implemented the ASP.NET user controls. 我实现了ASP.NET用户控件。

Each user can log in and update their personal data, for example on the private page myData.html 每个用户都可以登录并更新其个人数据,例如在私有页面myData.html

Now, I would like a part of their profile to be displayed on a public read-only page, for example - userGreg45public.html 现在,我希望将他们的个人资料的一部分显示在公共的只读页面上,例如userGreg45public.html

How do I implement 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) (请注意-有一组“文件”,每个用户都用相同的名称命名,因为它们只能在特定的人登录时才能访问。例如,myData.html的名称相同,但根据谁显示不同的数据登录。“公共页面”必须为每个成员具有唯一的文件名)

Thank You. 谢谢。

It sounds like the issue is you want each user to have their own url... Is this the case? 听起来好像是您要每个用户都有自己的URL的问题。

You can use URL Rewriting. 您可以使用URL重写。 One of the more simple approaches is to use the HttpContext.RewritePath in the Application_BeginRequest method of your Global.asax file. 一种更简单的方法是在Global.asax文件的Application_BeginRequest方法中使用HttpContext.RewritePath

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". 我在这里进行一些假设...我所做的假设是:a)用户使用名字,b)用户使用姓氏,c)用户使用一些唯一标识符,在我的情况下,我使用的是“ userId” 。

To start, you will have to decide on a format for the public urls. 首先,您必须确定公共URL的格式。 Something like this could work: 这样的事情可能会起作用:

http://www.yoursite.com/public/firstname-lastname-1234.aspx

Where 1234 is the userId. 其中1234是userId。

Next, you will want a function to build these URLs. 接下来,您将需要一个函数来构建这些URL。 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: 现在,在Global.asax文件中,添加以下代码:

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. 首先,通过简单地定义此函数,ASP.Net将其订阅HttpApplication.BeginRequest事件。

Next is the Regex which does the heavy lifting. 接下来是进行繁重工作的Regex。 It basically matches any requests to your a user profile page, and internally tells ASP.Net to route the request to another page. 它基本上将任何请求匹配到您的用户配置文件页面,并在内部告诉ASP.Net将请求路由到另一个页面。 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. 实施/public/userProfile.aspx页取决于您。 There are numerous ways to make things readonly. 有很多方法可以使事物变为只读。 It might be as straight forward as replacing TextBox controls with Label controls? 可能就像用Label控件替换TextBox控件一样直接?

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 ). (尽管在某些时候,最好使用诸如thisthis这样的组件)。

As an FYI, I referenced samples on this page for the rewrite stuff. 仅供参考,我在本页上引用了一些示例来进行重写。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

默认情况下,您可以为访问该站点的用户分配“来宾”等角色。

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. 它不允许进行编辑,并且位于任何人都可以查看的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net 3.5中创建装饰器代理页面 - how to create a decorator proxy page in asp.net 3.5 如何仅查询当前用户ID(VWD asp.net 3.5)的表数据? - How do I query table data only for the current user's ID (VWD asp.net 3.5)? 如何从ASP.NET 3.5中的用户控件返回成员数? - How do I return number of members from user controls in ASP.NET 3.5? asp.net用户配置文件:数据库拥有用户后,如何添加字段? - asp.net User Profile: how do I add a field once the DB has Users? 如何使用Asp.Net MVC通过配置文件创建安全策略? - How do I to create a security policy by profile using Asp.Net MVC? 如何在asp.net 3.5中将用户重定向到会话超时的默认页面 - how to redirect user to default page on session time out in asp.net 3.5 创建ASP.NET配置文件 - Create ASP.NET Profile 如何在当前用户的ASP.NET Identity 2中获取用户配置文件值? - How do you obtain user profile values in ASP.NET Identity 2 of current user? ASP.NET 3.5中的多页表单 - Multiple page form in asp.net 3.5 如何从 ASP.NET 用户控件中的父页面访问控件? - How do I access controls from the parent page from within an ASP.NET User Control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM