从appsettings配置节中读取值,并在预定义的位置注入值



我在控制器内部注入了IOptions

private readonly IOptions<MySettingsModel> appSettings; 
public MyController(IOptions<MySettingsModel> app)  
{  
appSettings = app;  
} 
[HttpGet]
public IActionResult Get()
{
var configFeedUrl = appSettings.PublicFeedUrl;
var requestMessage = new HttpRequestMessage(HttpMethod.Get, configFeedUrl);

this will make url to be 
v1/feed?key={feedKey}&location={access_location}

// how can I inject values here to take place on feedKey and access_location    
var myFeedKey = 123456;
var myAccessLocation = "Boston";

so that final url looks like this
var v1/feed?key=123456&location=Boston      

I do not want to change appsettings.json config.
}

你的问题与配置无关,它只是一个字符串操作

var f = appSettings.PublicFeedUrl;
// "v1/feed?key={feedKey}&location={access_location}";
var myFeedKey = 123456;
var myAccessLocation = "Boston";

var configFeedUrl = f.Replace("{feedKey}",myFeedKey.ToString())
.Replace("{access_location}",myAccessLocation);

结果

v1/feed?key=123456&location=Boston

最新更新