-1
我希望ASP.Net Core IoC容器根據當前顯示的視圖提供接口的實現。我想通過提供工廠方法,我應該可以在運行時做到這一點。在Startup.cs
我已經添加下列註冊:基於HttpContext的ASP.Net核心DI工廠
services.AddTransient<IMyInterface>(serviceProvider =>
{
var context = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
//Need to switch on current view somehow?
switch(context.??????){
case ???:
return new MyImplementationOne();
case ???:
return new MyImplementationTwo():
}
}
如何使用HttpContext
基於當前視圖切換被顯示?或者,我是否應該通過serviceProvider
使用其他一些服務?
你試過用請求的URL?在執行控制器操作並返回查看結果之前,我們無法知道將顯示哪個視圖。在那時,所有的依賴關係必須已經解決。 – juunas
@juunas我對此很陌生:如何獲得請求URL? 'context.Request'似乎沒有'URL'屬性呢? –
有一個'Path'屬性。 – juunas