ASP.net“BasePage”类的想法

添加到ASP.net BasePage : System.Web.UI.Page哪些很酷的function和方法BasePage : System.Web.UI.Page类?

例子

这是我用于身份validation的内容,我想听听您对此的意见:

 protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); // Authentication code omitted... Essentially same as below. if (_RequiresAuthentication && !(IsAuthorized)) { RespondForbidden("You do not have permissions to view this page.", UnauthorizedRedirect); return; } } // This function is overridden in each page subclass and fitted to each page's // own authorization requirements. // This also allows cascading authorization checks, // eg: User has permission to view page? No - base.IsAuthorized - Is user an admin? protected virtual bool IsAuthorized { get { return true; } } 

我的BasePage类包含此类的实例:

 public class StatusCodeResponse { public StatusCodeResponse(HttpContext context) { this._context = context; } ///  /// Responds with a specified status code, and if specified - transfers to a page. ///  private void RespondStatusCode(HttpContext context, System.Net.HttpStatusCode status, string message, string transfer) { if (string.IsNullOrEmpty(transfer)) { throw new HttpException((int)status, message); } context.Response.StatusCode = (int)status; context.Response.StatusDescription = message; context.Server.Transfer(transfer); } public void RespondForbidden(string message, string transfer) { RespondStatusCode(this._context, System.Net.HttpStatusCode.Forbidden, message, transfer); } // And a few more like these... } 

作为旁注,这可以使用HttpResponse对象的扩展方法来完成。

另一种方法我觉得解析querystring int参数非常方便:

 public bool ParseId(string field, out int result) { return (int.TryParse(Request.QueryString[field], out result) && result > 0); } 

  • 会话相关的东西,BasePage中映射到会话的一些复杂对象,并将其作为属性公开。
  • 做一些像填充碎屑垫物体的东西。

但最重要的是: 不要让你的基页成为一些帮助类 。 不要添加像ParseId()这样的东西,这太荒谬了。


另外,基于第一篇文章:制作像IsAuthorized 抽象的东西。 这样,如果有人忘记了某些虚拟方法,就不会创建巨大的安全漏洞。

 using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; namespace MySite { ///  /// Base class with properties for meta tags for content pages /// http://www.codeproject.com/KB/aspnet/PageTags.aspx /// http://weblogs.asp.net/scottgu/archive/2005/08/02/421405.aspx ///  public partial class BasePage : System.Web.UI.Page { private string keywords; private string description; ///  /// Gets or sets the Meta Keywords tag for the page ///  public string Meta_Keywords { get { return keywords; } set { // Strip out any excessive white-space, newlines and linefeeds keywords = Regex.Replace(value, "\\s+", " "); } } ///  /// Gets or sets the Meta Description tag for the page ///  public string Meta_Description { get { return description; } set { // Strip out any excessive white-space, newlines and linefeeds description = Regex.Replace(value, "\\s+", " "); } } // Constructor // Add an event handler to Init event for the control // so we can execute code when a server control (page) // that inherits from this base class is initialized. public BasePage() { Init += new EventHandler(BasePage_Init); } // Whenever a page that uses this base class is initialized, // add meta keywords and descriptions if available void BasePage_Init(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Meta_Keywords)) { HtmlMeta tag = new HtmlMeta(); tag.Name = "keywords"; tag.Content = Meta_Keywords; Header.Controls.Add(tag); } if (!String.IsNullOrEmpty(Meta_Description)) { HtmlMeta tag = new HtmlMeta(); tag.Name = "description"; tag.Content = Meta_Description; Header.Controls.Add(tag); } } } } 

除了已经提到的元数据( 在ASP.NET 4.0中已经过时使用新的Page.MetaDescription和Page.MetaKeywords属性),我还有一些方法可以向我的页面添加其他标题链接,例如用于添加页面特定CSS的特定标题链接,或类似于链接,RSS链接等的东西:

 ///  /// Adds a CSS link to the page. Useful when you don't have access to the /// HeadContent ContentPlaceHolder. This method has 4 overloads. ///  ///  /// Adds a CSS link. ///  /// The path to CSS file. public void AddCss(string pathToCss) { AddCss(pathToCss, string.Empty); } ///  /// Adds a CSS link in a specific position. ///  /// The path to CSS. /// The postion. public void AddCss(string pathToCss, int? position) { AddCss(pathToCss, string.Empty, position); } ///  /// Adds a CSS link to the page with a specific media type. ///  /// The path to CSS file. /// The media type this stylesheet relates to. public void AddCss(string pathToCss, string media) { AddHeaderLink(pathToCss, "text/css", "Stylesheet", media, null); } ///  /// Adds a CSS link to the page with a specific media type in a specific /// position. ///  /// The path to CSS. /// The media. /// The postion. public void AddCss(string pathToCss, string media, int? position) { AddHeaderLink(pathToCss, "text/css", "Stylesheet", media, position); } ///  /// Adds a general header link. Useful when you don't have access to the /// HeadContent ContentPlaceHolder. This method has 3 overloads. ///  ///  /// Adds a general header link. ///  /// The path to the resource. /// The type of the resource. public void AddHeaderLink(string href, string type) { AddHeaderLink(href, type, string.Empty, string.Empty, null); } ///  /// Adds a general header link. ///  /// The path to the resource. /// The type of the resource. /// The relation of the resource to the page. public void AddHeaderLink(string href, string type, string rel) { AddHeaderLink(href, type, rel, string.Empty, null); } ///  /// Adds a general header link. ///  /// The path to the resource. /// The type of the resource. /// The relation of the resource to the page. /// The media target of the link. public void AddHeaderLink(string href, string type, string rel, string media) { AddHeaderLink(href, type, rel, media, null); } ///  /// Adds a general header link. ///  /// The path to the resource. /// The type of the resource. /// The relation of the resource to the page. /// The media target of the link. /// The postion in the control order - leave as null /// to append to the end. public void AddHeaderLink(string href, string type, string rel, string media, int? position) { var link = new HtmlLink { Href = href }; if (0 != type.Length) { link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(), type); } if (0 != rel.Length) { link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), rel); } if (0 != media.Length) { link.Attributes.Add("media", media); } if (null == position || -1 == position) { Page.Header.Controls.Add(link); } else { Page.Header.Controls.AddAt((int)position, link); } } 

通过重写InitializeCulture()方法进行文化InitializeCulture()从cookie或DB设置culture和ui culture)。

我的一些应用程序是可品牌化的,然后在这里我做了一些“品牌”的东西。

我用这个methot并感谢你的,

 ///  /// Displays the alert. ///  /// The message to display. protected virtual void DisplayAlert(string message) { ClientScript.RegisterStartupScript( GetType(), Guid.NewGuid().ToString(), string.Format("alert('{0}');", message.Replace("'", @"\'")), true ); } ///  /// Finds the control recursive. ///  /// The id. /// control protected virtual Control FindControlRecursive(string id) { return FindControlRecursive(id, this); } ///  /// Finds the control recursive. ///  /// The id. /// The parent. /// control protected virtual Control FindControlRecursive(string id, Control parent) { if (string.Compare(parent.ID, id, true) == 0) return parent; foreach (Control child in parent.Controls) { Control match = FindControlRecursive(id, child); if (match != null) return match; } return null; } 

将授权代码放在基页中通常不是一个好主意。 问题是,如果您忘记从基页派生需要授权的页面会发生什么? 你会有一个安全漏洞。

使用HttpModule要好得多,这样您就可以拦截所有页面的请求,并确保在HttpHandler有机会运行之前授权用户。

此外,正如其他人所说,并且为了与OO原则保持一致,最好只在基页中使用与页面本身相关的方法。 如果他们没有引用“this”,那么他们应该是一个帮助类 – 或者可能是扩展方法。

当我需要某些属性和每个页面时,我从System.Web.UI.Pageinheritance。 这对于实现登录的aweb应用程序很有用。 在成员资格页面中,我使用自己的基类来访问属性,如UserID,UserName等。这些属性包装会话变量

以下是我使用自定义基类的一些示例(无代码):

  1. 添加页面filter(例如将“{theme}”替换为“〜/ App_Theme / [currentTheme]”),
  2. 根据站点地图添加属性并处理自动标题页面,
  3. 注册专门的日志记录(可能通过不同方式重做),
  4. 使用毯式重定向器添加通用输入(Form / Querystring)validation的方法:AddRequiredInput(“WidgetID”,PageInputType.QueryString,typeof(string)),
  5. 站点地图帮助程序,允许将静态“编辑类”更改为与“编辑秋季’10科学101”相关的上下文
  6. ViewState Helpers,允许我将页面上的变量注册到名称,并让它自动从视图状态或默认值填充该变量,并在请求结束时将值保存回viewstate。
  7. 自定义404重定向器,我可以在其中传递exception或消息(或两者),它将转到我已预定义的页面,以便很好地显示和记录它。

我个人最喜欢#5因为a)更新SiteMap是丑陋的,我不喜欢弄乱页面,使其更具可读性; b)它使SiteMap更加用户友好。

请参阅ASP.Net Base Page中的获取页面特定信息

 public abstract string AppSettingsRolesName { get; } List authorizedRoles = new List((ConfigurationManager.AppSettings[AppSettingsRolesName]).Split(',')) if (!authorizedRoles.Contains(userRole)) { Response.Redirect("UnauthorizedPage.aspx"); } 

在派生的页面

 public override string AppSettingsRolesName { get { return "LogsScreenRoles"; } }