简体   繁体   English

在这里我们可以重写.net基类库方法

[英]where we can override .net base class library methods

I need to override default Functions of Base class to implement some custom behavior. 我需要重写基类的默认函数来实现一些自定义行为。 For example i need to handle unauthorized requests in an MVC4 Application. 例如,我需要处理MVC4应用程序中的未授权请求。 I searched and found some answer which are overriding default methods of AuthorizeAttribute class as below: 我搜索并找到一些答案,这些答案覆盖了AuthorizeAttribute类的默认方法,如下所示:

Default basse class: 默认贝司等级:

public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{

    public AuthorizeAttribute();

    public string Roles { get; set; }

    public override object TypeId { get; }

    public string Users { get; set; }

    protected virtual bool AuthorizeCore(HttpContextBase httpContext);

    protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext);

    public virtual void OnAuthorization(AuthorizationContext filterContext);

    protected virtual HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext);
}

Overriding HandleUnauthorizedRequest: 覆盖HandleUnauthorizedRequest:

public class CustomAuthorizationAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        // You need to set this action result to something other than a HttpUnauthorizedResult, 
        // this result will cause the redirection to the login page

        // Forbidden request... does not redirect to login page
        // filterContext.Result = new HttpStatusCodeResult(403);

        filterContext.Result = new ErrorActionResult { ErrorMessage = "Unauthorized Access" };
    }
}

But i have few questions in mind that i would like to get explained before using this functionlality. 但是我在想使用此功能之前先要解释几个问题。 I know these are very basic questions but still have some confusion in mind about these. 我知道这些是非常基本的问题,但在这些方面仍存在一些困惑。

Questions: 问题:

  1. Where we can overide Base class methods, Means do i need to add a new c# class file and then override BCL Methods? 在哪里可以覆盖基类方法,我是否需要添加一个新的c#类文件,然后覆盖BCL方法?

  2. if i override, then i have to use [CustomAuthorization] signature on all methods where i have used [Authorize] signature. 如果重写,则必须在使用[Authorize]签名的所有方法上使用[CustomAuthorization]签名。 Is there any way to overirde default functionality of HandleUnauthorizedRequest without making any signature change? 有没有办法改变HandleUnauthorizedRequest的默认功能而不进行任何签名更改?

  3. if we have to override functionality of some default BCL Methods which is required for some particular page liek .ToString() then should we override these BCL methods in that class itself. 如果我们必须重写某些特定页面liek .ToString()所需的某些默认BCL方法的功能,则应在该类本身中重写这些BCL方法。 Is it correct ? 这是正确的吗 ?

Any answers or suggestions will be appreciated. 任何答案或建议,将不胜感激。 Thank you. 谢谢。

Gerry. 格里 In regards to overriding base class methods, you can only do so if the method is marked as virtual. 关于覆盖基类方法,仅当该方法被标记为虚拟方法时,您才能这样做。 Yes, you need to inherit from the base class, and override the virtual methods as necessary. 是的,您需要从基类继承,并在必要时覆盖虚拟方法。 You can hide non-virtual methods by making use of the 'new' keyword. 您可以使用'new'关键字来隐藏非虚拟方法。 Also, classes marked as sealed cannot be inherited. 同样,标记为密封的类不能继承。

Yes, you will need to create a custom Auth attribute, and change all your usages, although an alternative would be to register it as a global filter, but that would then be applied to every action, which you may not want. 是的,您将需要创建一个自定义Auth属性,并更改所有用法,尽管一种替代方法是将其注册为全局过滤器,但是随后将其应用于您可能不需要的所有操作。

You are correct, if you want to override something like .ToString(), then you do it the child/inheriting classes. 您是正确的,如果您想覆盖.ToString()之类的东西,则可以将其作为子类/继承类。

Q1. Q1。 Yes

Q2. Q2。 Yes and no. 是的,没有。 Normally to use the subclass, you'd have to make it explicit, ie replace all existing attributes with subclass attributes. 通常,要使用子类,您必须使其明确,即用子类属性替换所有现有属性。

However, MVC is kind of specific as it has been designed so that it allows you to hook into many internals. 但是,MVC是特定于其设计的,因此它使您可以插入许多内部组件。 In particular, the internal resolver, that is called when infrastructure elements are created, can be customized. 特别是,可以自定义内部解析器,即在创建基础结构元素时调用的解析器。

Take a look at this tutorial 看一下本教程

http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise3 http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise3

First, they set up a custom dependency resolver ( IDependencyResolver ). 首先,他们建立了一个自定义的依赖解析器( IDependencyResolver )。 The resolver is called by MVC everytime it needs something. 每当需要时,MVC都会调用解析程序。 When filters are about to be used, MVC asks the resolver for a IFilterProvider . 当将要使用过滤器时,MVC向解析器询问IFilterProvider The provider then is responsible for creating a filter. 然后,提供者负责创建过滤器。 And this is where your custom filter provider could be smart and return your inherited class instances instead of base class instances. 这是您的自定义过滤器提供程序可以很聪明的地方,它可以返回继承的类实例而不是基类实例。

Although technically possible, the question remains - would it be clean and understandable enough? 尽管从技术上讲是可行的,但问题仍然存在-它是否干净和易于理解? Since the resolver is configured externally to controllers/actions, just looking at controllers and actions code would not reveal that filters are replaced by the provider. 由于解析器是在控制器/操作外部配置的,因此仅查看控制器和操作代码将不会显示过滤器已提供程序替换 This doesn't sound good, sooner or later someone could possibly make a major mistake because of this confusion. 这听起来不太好,由于这种混乱,迟早有人可能会犯重大错误。 The recommendation would be then to make it explicit - use subclass filters in an explicit way. 建议是使其明确-以明确的方式使用子类过滤器。

Q3. Q3。 Yes

I think most of your question comes down to: 我认为您的大部分问题归结为:

can I monkey-patch in .NET? 我可以在.NET中进行猴子补丁吗?

to which the answer is simply "no". 答案就是“否”。 You must use known and published extension points. 您必须使用已知和已发布的扩展点。 Sometimes that means override in a sub-class; 有时,这意味着在子类中进行override sometimes that means changing some provider (usually implementing an interface), and sometimes it means hooking an event. 有时,这意味着更改某些提供程序(通常实现接口),有时,这意味着挂接事件。 Sometimes, you simply can't do it (although in the specific example of MVC4, even if there isn't an extension point you could just fork the source code itself and recompile). 有时,您根本做不到 (尽管在MVC4的特定示例中,即使没有扩展点,您也可以只分叉源代码本身并重新编译)。

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

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