MVC 6 WebAPI FromServices 属性无法编译

本文关键字:编译 属性 FromServices WebAPI MVC | 更新日期: 2023-09-27 18:33:55

我正在遵循本教程:http://www.mithunvp.com/create-aspnet-mvc-6-web-api-visual-studio-2015/

哪个应该是最新的,应该在 rc2 上工作

信息:操作系统: 赢 10 x64 14257VS:15 更新 1DNX: 1.0.0-rc2-16357 clr x86

错误:

CS0592  Attribute 'FromServices' is not valid on this declaration type. It is only valid on 'parameter' declarations.

WebAPIController

[Route("api")]
public class WebAPIController : Controller
{
    [FromServices]
    IWebAPIRepository WebAPI { get; set; }
    [HttpPost("Authenticate", Name = "Authenticate")] // /api/Authenticate
    public async Task<IActionResult> Authenticate([FromBody] LoginViewModel model)
    {
        if (model == null)
            return Ok(new { success = false });
        AuthenticationStatus result = await WebAPI.Authenticate(HttpContext.Authentication, model.Email, model.Password, model.RememberMe);
        switch (result)
        {
            case AuthenticationStatus.Success:
                return Ok(new { success = true });
            case AuthenticationStatus.LockedOut:
                return Ok(new { success = false, error = "Locked out" });
            case AuthenticationStatus.Failure:
            default:
                return Ok(new { success = false, error = "Invalid login attempt" });
        }
    }
}

该教程应该没问题

WebAPI 存储库(界面包含在同一文件中,位置模型文件夹)

public interface IWebAPIRepository
{
    Task<AuthenticationStatus> Authenticate(AuthenticationManager manager, string email, string password, bool rememberMe);
}
public enum AuthenticationStatus
{
    Success,
    LockedOut,
    Failure
}
public class WebAPIRepository : IWebAPIRepository
{
    public async Task<AuthenticationStatus> Authenticate(AuthenticationManager manager, string email, string password, bool rememberMe)
    {
        // ......
        await manager.SignInAsync("Cookies", new ClaimsPrincipal(id));
        return AuthenticationStatus.Success;
    }
 }

最后是启动.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IWebAPIRepository, WebAPIRepository>();
    }
    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseDeveloperExceptionPage();
        app.UseCookieAuthentication(options =>
        {
            options.LoginPath = "/account/login";
            options.AccessDeniedPath = "/account/forbidden";
            options.AuthenticationScheme = "Cookies";
            options.AutomaticAuthenticate = true;
            options.AutomaticChallenge = true;
        });
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        app.UseStaticFiles();
    }
    public static void Main(string[] args)
    {
        var application = new WebApplicationBuilder()
            .UseConfiguration(WebApplicationConfiguration.GetDefault(args))
            .UseStartup<Startup>()
            .Build();
        application.Run();
    }

可能出了什么问题?

MVC 6 WebAPI FromServices 属性无法编译

所以我找到了解决方案:

IWebAPIRepository WebAPI { get; set; }
public WebAPIController([FromServices] IWebAPIRepository API)
{
        WebAPI = API;
}

希望这对其他人有所帮助:))