简体   繁体   English

ASP.Net Core 2.2 - 方法重载出现在 Visual Studio 中,但在运行时不起作用

[英]ASP.Net Core 2.2 - Method overload appears in Visual Studio but does not work at run-time

I am attempting to verify that a user is authorized via a custom policy.我正在尝试验证用户是否通过自定义策略获得授权。 I followed the tutorial at Ode To Code to add this functionality to my controller.我按照Ode To Code 中的教程将此功能添加到我的控制器中。 From within Visual Studio, the code appears to be correct and utilizing a known overload.在 Visual Studio 中,代码似乎是正确的,并且使用了已知的重载。

Visual Studio 屏幕截图

Notice that it says that the overload is an "extension".请注意,它说重载是一个“扩展”。 I didn't take much notice of this until I spent 5 hours today trying to solve the following error:直到今天我花了 5 个小时试图解决以下错误,我才注意到这一点:

Asp.net 错误

As you can see, it would appear that the overload I'm attempting to use isn't being utilized.如您所见,我尝试使用的重载似乎没有被利用。 Am I doing something wrong here?我在这里做错了吗? Is there something special I have to do to include these extended methods?为了包含这些扩展方法,我需要做什么特别的事情吗? I've attempted cleaning and rebuilding the solution but this hasn't solved the problem.我已经尝试清理和重建解决方案,但这并没有解决问题。

While you've defined the field for IAuthorizationSerivce , you haven't provided any way for that to be set.虽然您已经为IAuthorizationSerivce定义了字段, IAuthorizationSerivce您还没有提供任何设置方法。 You need to define a constructor for the LRController that takes a single parameter of IAuthorizationService , and assign that to the field.您需要定义的构造LRController ,它利用一个参数IAuthorizationService ,并分配到外地。

I think there was a definition of that constructor in the tutorial.我认为教程中有该构造函数的定义。

Please note the name change: such as the global variable name for IAuthorizationService _authorization has been prefixed with an underscore.请注意名称更改:例如 IAuthorizationService _authorization的全局变量名称已以下划线作为前缀。 Obviously not required, but as a good rule of thumb/good coding standard, IMO.显然不是必需的,但作为一个很好的经验法则/良好的编码标准,IMO。 :-) :-)

public class LRController : Controller
{
    private readonly IAuthorizationService _authorization;

    // you're missing this constructor & this pattern is known as Constructor Dependency Injection
    public LRController(IAuthorizationService authorization)
    {
        _authorization = authorization;
    }

    public async Task<RedirectToActionResult> Index()
    {
        var superAdmin = await _authorization.AuthorizeAsync(User, "IsLucky");
        //rest of your code here
    }

}

EDIT编辑

Additionally, if you wanted/needed to inject other interfaces into this controller, you would add it to that LRController constructor.此外,如果您想/需要将其他接口注入该控制器,您可以将其添加到该 LRController 构造函数中。 Would look something like this:看起来像这样:

public class LRController : Controller
{

    private readonly IAuthorizationService _authorization;
    private readonly IOtherService _otherService;

    public LRController(IAuthorizationService authorization, IOtherService otherService)
    {
        _authorization = authorization;
        _otherService = otherService;
    }

    public async Task<RedirectToActionResult> Index()
    {
        var superAdmin = await _authorization.AuthorizeAsync(User, "IsLucky");
    }

    public async Task Foo()
    {
        await _otherService.Bar();
    }

}

暂无
暂无

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

相关问题 Asp.Net Core 2.2由当前用户更改数据库运行时 - Asp.Net Core 2.2 change database run-time by current user ASP.Net Core应用程序可在Visual Studio中运行,但不能与dotnet运行 - ASP.Net Core app works in visual studio but not with dotnet run 通过Visual Studio的ASP.NET MVC核心部署不起作用 - ASP.NET MVC Core Deployment via Visual Studio does not work Visual Studio Code 调试默认的 ASP.NET Core MVC WebApp:不起作用 - Visual Studio Code debugging the default ASP.NET Core MVC WebApp: does not work 如何在运行时重新加载自定义属性? ASP.NET核心MVC - How to reload a Custom Attribute during run-time? ASP.NET Core MVC 热门在运行时更改ASP.NET Core Angular应用程序中的主题 - Hot to change theme at run-time in ASP.NET Core Angular application 如何在运行时使用ASP.NET Core中的密码获取连接字符串 - How to get connection string in run-time with password in ASP.NET Core ASP.NET Core 2.2 Razor Pages 为什么 OnPost() 有效,而 OnPostAsync() 无效? - ASP.NET Core 2.2 Razor Pages why does OnPost() work, but OnPostAsync() doesn't? 覆盖不适用于ASP.net Core MVC中的虚拟方法 - Overriding does not work on virtual method in ASP.net Core MVC “IServiceCollection”不包含“AddAWSService”的定义,并且在asp.net core 2.2 中没有可访问的扩展方法“AddAWSService”错误? - 'IServiceCollection' does not contain a definition for 'AddAWSService' and no accessible extension method 'AddAWSService' error in asp.net core 2.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM