简体   繁体   English

如何在aspx.cs页面上使用MVC自定义属性?

[英]How to use MVC custom attribute on aspx.cs page?

My application is in Asp.net MVC 4. I'm using .aspx page for opening of report. 我的应用程序在Asp.net MVC 4中。我正在使用.aspx页打开报告。 I've implemented custom user rights attribute on all action of application. 我已经在应用程序的所有操作上实现了自定义用户权限属性。 I want to use it on my .aspx.cs page class or every function that is in .aspx.cs page. 我想在我的.aspx.cs页面类或.aspx.cs页面中的每个函数上使用它。 How to use it? 如何使用它? can i use MVC custom attribute in aspx page In mvc I'm using like this 我可以在MVC中的aspx页面中使用MVC自定义属性吗?

[AuthorizeUserControls("urlInventoryReport")]   
public ActionResult Inventory(string ReportTitle)    
{

}

How to use in .aspx.cs page 如何在.aspx.cs页中使用

public partial class ReportViewer : System.Web.UI.Page

{

[AuthorizeUserControls("urlInventoryReport")] //it's not working

private void ViewInventoryReport()

{

}

}

Attributes are static objects that apply metadata to a type in .NET. 属性是将元数据应用于.NET中的类型的静态对象。 They contain no behavior . 它们不包含任何行为

The reason why your attribute works in ASP.NET MVC is because MVC has a filter which runs before and after the call to the action method is performed. 属性在ASP.NET MVC中起作用的原因是,MVC具有一个过滤器 ,该过滤器在对action方法的调用执行之前和之后运行。 This filter is called by the MVC framework, which in turn is called by the route handler (a specialized HTTP handler). 此筛选器由MVC框架调用,而MVC框架又由路由处理程序(专用的HTTP处理程序)调用。

The fact that the behavior is defined in the same class as the attribute (yielding an ActionFilterAttribute) is just for convenience. 为方便起见,行为是在与属性相同的类中定义的(生成一个ActionFilterAttribute)。 You could just as well separate the attribute from the action filter as is done in this answer . 您可以像在此答案中一样将属性与动作过滤器分开。

Following the MVC approach to make your IActionFilter function, would be to use .NET routing for your page and make a specialized IRouteHandler that can scan your page object after it is instantiated using Reflection to determine if the attribute exists, and then execute the behavior in the associated IActionFilter . 按照MVC的方法创建IActionFilter函数的方法是,对页面使用.NET路由,并创建专门的IRouteHandler ,该对象可以在使用Reflection实例化页面对象后扫描页面对象以确定该属性是否存在,然后在其中执行行为。关联的IActionFilter I suggest if you go this route, you analyze the MVC source code and extract the bits that you need, but it is not for the faint of heart. 我建议您走这条路,分析MVC源代码并提取所需的位,但这不是出于胆小。

Alternatively, you could put the scanning implementation into the Page_Init event, but at that point you might just be better off not bothering with declaring the attribute statically and just executing the behavior locally. 另外,您可以将扫描实现放入Page_Init事件中,但此时最好不要烦于静态声明属性和仅在本地执行行为。

Assuming your attribute derives from ActionFilterAttribute , you could do something like: 假设您的属性是从ActionFilterAttribute派生的,则可以执行以下操作:

protected void Page_Init(object sender, EventArgs e)
{
    var attribute = new AuthorizeUserControls("urlInventoryReport");
    var filterContext = CreateFakeActionExecutingContext(); // TODO: Implement this.
    attribute.OnActionExecuting(filterContext);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM