Create and Use Custom Attributes
2024-10-19 16:41:39
http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat
Create a custom attribute class:
[AttributeUsage(AttributeTargets.Class)] // this attribute can only be used by class
public class RequirePermissionAttribute : Attribute { public string Module { get; set; } public string Function { get; set; } public RequirePermissionAttribute(string moduleId, string function) { if(string.IsNullOrEmpty(moduleId)) throw new ArgumentException("'Module' cannot be empty.", "Module"); this.Module = moduleId; this.Function = function; } }
Using in class:
[RequirePermission(")] public partial class WebForm2 : BasePage { protected void Page_Load(object sender, EventArgs e) { } }
How to use in BasePage.cs
RequirePermissionAttribute[] attributes = (RequirePermissionAttribute[])Page.GetType().GetCustomAttributes(typeof(RequirePermissionAttribute), true); ) { Response.Redirect(ResolveUrl("~/Error.html")); } else { RequirePermissionAttribute attribute = attributes[]; string moduleId = attribute.Module; string function = attribute.Function; //validate if has permission by module id and function }
request.UrlReferrer
INPUT
Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
OUTPUT
localhost
localhost:60527
/WebSite1test/Default2.aspx
/WebSite1test
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
最新文章
- C#实现二维码功能,winform 以及 asp.net均可以用
- duilib的caption上的Edit无法激活
- linux常用命令之文件系统
- mysql学习笔记 第八天
- spring mvc中的valid
- 解决ASP.NET使用IIS架设网站时“服务器应用程序不可用”的方法
- [Objective-c 基础 - 2.5] .h和.m文件,点语法,成员变量作用域
- mysql主从监控
- poj 3308 (最大流)
- Windows 下统计行数的命令
- 【QT相关】Qt Widgets Module
- SystemTap----将SystemTap脚本编译成内核模块
- Python中闭包、装饰器的概念
- Ubuntu与Centos在登陆安全方面的比较
- [Swift]LeetCode46. 全排列 | Permutations
- scala查询dataFrame结构
- python 进程/线程/协程 测试
- centos7搭建ELK Cluster集群日志分析平台(二):Logstash
- python网络爬虫笔记(四)
- 【转】PHP 杂谈 坑爹的file_exists