.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 }
本文来自博客园,作者:Sir-Xu,转载请注明原文链接:https://www.cnblogs.com/DevinXu/p/15949117.html
浙公网安备 33010602011771号