.Net Core MVC像以前一样添加依赖

 1 using Microsoft.AspNetCore.Mvc;
 2 using Microsoft.AspNetCore.Mvc.ModelBinding;
 3 
 4 namespace App
 5 {
 6     [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
 7     public class PropertyFromServiceAttribute : Attribute, IBindingSourceMetadata
 8     {
 9         public BindingSource BindingSource => BindingSource.Services;
10     }
11 
12     public class GreetingController : ControllerBase
13     {
14         [PropertyFromService] public IGreeter greeter { get; set; }
15 
16         public GreetingController()
17         {
18 
19         }
20         [HttpGet("/greet")]
21         //public string Greet([FromServices] IGrIGreetereeter greeter) => greeter.Greet(DateTimeOffset.Now)
22         public string Greet() {
23             return null;
24         }
25     }
26 }

 

posted @ 2022-03-01 10:40  Sir-Xu  阅读(32)  评论(0)    收藏  举报