How to use HttpContext.Current in ASP.NET Core - c#

I need to get HttpContext.Current and HostingEnvironment in a class EmployeeDataAccessLayer in AddEmployee function. I have written below code now I am facing a problem that how can I call/use the function AddEmployee in my controller.
Now, as I have created 2 new constructors with parameters IHttpContextAccessor and IHostingEnvironment respectively causing me problem, I am not getting a proper way to use it.
public class EmployeeDataAccessLayer
{
private readonly IHttpContextAccessor _httpContextAccessor;
private IHostingEnvironment _hostingEnvironment;
public EmployeeDataAccessLayer(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public EmployeeDataAccessLayer(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public void AddEmployee(TblEmployee employee)
{
try
{
string folderName = "UploadFile/";
string sPath = "";
sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);
var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
}
I am following this article.

Most likely you haven't configured your controller to require an instance of EmployeeDataAccessLayer.
Be sure to register the EmployeeDataAccessLayer as a dependency like in the article you linked. Then your controller should take EmployeeDataAccessLayer as a constructor argument, you will store that as a readonly field and use it in your controller action. Then you should see that EmployeeDataAccessLayer has an instance of IHttpContextAccessor provided.
See a more complete example as the one you link is not complete(e.g. this one from microsoft).
As a side note, in your EmployeeDataAccessLayer you probably should not require the IHttpContext dependency if possible as others have mentioned in comments.

Try using interface as follows :
//controller
public class HomeController
{
private readonly IDataAccess _dataAccess;
public HomeController(IDataAccess dataAccess)
{
_dataAccess = dataAccess;
}
[HttpPost]
public ActionResult Index(TblEmployee employee)
{
_dataAccess.AddEmployee(employee);
return View();
}
}
// Startup
public void ConfigureServices(IServiceCollection services)
{
// add dependency
services.AddScoped<IDataAccess, EmployeeDataAccessLayer>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// Data Access Impl
public class EmployeeDataAccessLayer : IDataAccess
{
private readonly IHttpContextAccessor _httpContextAccessor;
private IHostingEnvironment _hostingEnvironment;
public EmployeeDataAccessLayer(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public EmployeeDataAccessLayer(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public void AddEmployee(TblEmployee employee)
{
try
{
string folderName = "UploadFile/";
string sPath = "";
sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);
var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
catch{}
}
}
// interface
public interface IDataAccess
{
void AddEmployee(TblEmployee employee);
}
Another ugly approach (Using service locator):
if you don't want DI and constructor, you can use service locator as follows:
public static class MyServiceLocator
{
public static IServiceProvider Instance { get; set; }
}
public void Configure(IApplicationBuilder app)
{
MyServiceLocator.Instance = app.ApplicationServices;
}
// Data Access
public class EmployeeDataAccessLayer
{
public void AddEmployee(TblEmployee employee)
{
try
{
IHttpContextAccessor httpContextAccessor =MyServiceLocator.Instance.GetService<IHttpContextAccessor>();
IHostingEnvironment hostingEnvironment=MyServiceLocator.Instance.GetService<IHostingEnvironment>();;
string folderName = "UploadFile/";
string sPath = "";
sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);
var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
}
catch{}
}
}

Related

How to create a global variable with DI?

I want to attempt something and I hope it is possible. I want a global property, containing the site base URL, that I can use across my application dlls. Here is what I thought of so far, while configuring my Dependency Injection:
public static IServiceCollection AddOrderEasyDependencies(this IServiceCollection services, IConfiguration configuration)
{
// Set golbal properties and configurations
var xxx = services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
RunningConfig runningConfig = new(xxx);
services.AddSingleton<IRunningConfig>(runningConfig);
}
Then the RunningConfig looks like this:
public class RunningConfig: IRunningConfig
{
private readonly IHttpContextAccessor _httpContextAccessor;
public string SiteUrl {
get
{
var httpContext = _httpContextAccessor.HttpContext;
var url = httpContext.Request.Scheme + "://" + httpContext.Request.Host.Value;
return url;
}
}
public RunningConfig(IServiceCollection services)
{
_httpContextAccessor = services.BuildServiceProvider().GetService<IHttpContextAccessor>();
}
}
IRunningConfig is just an interface with a property.
I have services.AddHttpContextAccessor(); in my ConfigureServices method in the startup class.
My problem is that IHttpContextAccessor is always null no matter how I do my injection. Please tell me what I am doing wrong or suggest a better way to do something like this.
I call the IRunningConfig like this:
public class SomeService: IService
{
private readonly IRunningConfig _runningConfig;
public SomeService(IServiceProvider services)
{
_runningConfig = services.GetService<IRunningConfig>();
}
public void DoMethodStuff()
{
var urlAddress = _runningConfig.SiteUrl;
}
}
Why are you using services.BuildServiceProvider()? Every time you are building new Provider and Singleton is not realy Singleton any more. You have to use DI
I would do it like this
public static IServiceCollection AddOrderEasyDependencies(this IServiceCollection services, IConfiguration configuration)
{
// Set golbal properties and configurations
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// For IHttpContextAccessor registration there is an extention mehod in Microsoft.Extensions.DependencyInjection
// services.AddHttpContextAccessor();
services.AddSingleton<IRunningConfig, RunningConfig>();
}
public class RunningConfig: IRunningConfig
{
private readonly IHttpContextAccessor _httpContextAccessor;
public string SiteUrl {
get
{
var httpContext = _httpContextAccessor.HttpContext;
var url = httpContext.Request.Scheme + "://" + httpContext.Request.Host.Value;
return url;
}
}
public RunningConfig(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
}

Access Session in Class using .Net Core 3.1 Razor Pages Application

I wish to check various session values from within my custom class. One thing I want to test is if the LoggedIn variable is set when a user hits a specific page. I want to do this in a class so I don't have to repeat code.
Here is how I've registered the service / IHttpContextAccessor:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<clsSessionHelper>();
services.AddRazorPages();
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/App/Mpl/MplHome/MplHome", "");
});
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(1200);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// End Session Support
services.AddMemoryCache();
}
Here is how I've build the custom class:
public class clsSessionHelper
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ISession _session;
public clsSessionHelper(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_session = _httpContextAccessor.HttpContext.Session;
}
public string getValue(string SessionKey)
{
string strResult = _session.GetString(SessionKey);
return strResult;
}
When I try to use the my helper class in a xx.cshtml.cs file like be below
clsSessionHelper objSessionHelper = new clsSessionHelper();
Test = objSessionHelper.getValue("LoggedIn");
I get an intellsense error "there is no argument given that corresponds to the required formal parameter 'httpContextAccessor' of 'clsSessionHelper.clsSessionHelper(HttpContextAccessor)'.
No doubt, I'm messing up the dependency injection. Any help would be greatly appreaciated.
The issue is that at the time the custom class is initialized and the IHttpContextAccessor injected, it is too early in the request process to have access to the HttpContext and by extension the ISession
Move any code that depend on the HttpContext out of the construct and into a member that is invoked during the context of a request.
public class clsSessionHelper : ISessionHelper {
private readonly IHttpContextAccessor httpContextAccessor;
public clsSessionHelper(IHttpContextAccessor httpContextAccessor) {
this.httpContextAccessor = httpContextAccessor;
}
public string getValue(string SessionKey) {
ISession session = httpContextAccessor.HttpContext.Session;
string strResult = session.GetString(SessionKey);
return strResult;
}
}
Also abstract your custom class
public interface ISessionHelper {
string getValue(string SessionKey);
}
so it too can be injected where needed.
//...
services.AddScoped<ISessionHelper, clsSessionHelper>();
//...
Avoid trying to initialize it manually.
private readonly ISessionHelper objSessionHelper;
public MyPageModel(ISessionHelper objSessionHelper) {
this.objSessionHelper = objSessionHelper;
}
public async Task<IActionResult> OnGet() {
var Test = objSessionHelper.getValue("LoggedIn");
//...
}

How to customise configuration binding in ASP.NET Core

I have a multi-tenant ASP.NET Core web application. The current tenancy model is every tenant has a separate web app and SQL database. I'm trying to rearchitect it so that multiple tenants will be served by a single web app (but maintaining a separate database per tenant). I've been following this series of blog posts but I've hit a bit of a roadblock with configuration.
The app makes heavy use of the ASP.NET Core configuration system, and has a custom EF Core provider that fetches config values from the database. I'd like to preserve this if possible, it would be an awful lot of work to rip out and replace with something else (dozens of config settings used in hundreds of places).
The existing code is very standard:
public class MyAppSettings
{
public string FavouriteColour { get; set; }
public int LuckyNumber { get; set; }
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
// etc....
}
}
// custom EF Core config provider wired up in Program.Main, but that doesn't actually seem relevant
I've already updated our custom provider so that it fetches all configuration values from all known tenant databases, and adds them all to the configuration system, prefixed with a tenant identifier, so the list of all config values fetched from the n different databases might look something like this:
Key Value
===============================================
TenantABC:MyAppSettings:FavouriteColour Green
TenantABC:MyAppSettings:LuckyNumber 42
TenantDEF:MyAppsettings:FavouriteColour Blue
TenantDEF:MyAppSettings:LuckyNumber 37
...
TenantXYZ:MyAppSettings:FavouriteColour Yellow
TenantXYZ:MyAppSettings:LuckyNumber 88
What I'd like to be able to do is somehow customise the way that the configuration is bound so that it resolves the tenant for the current request, and then uses the appropriate values, e.g. a request on abc.myapp.com would observe config values "Green" and "42", etc, without having to change all the dependent places that inject IOptionsMonitor<AppSettings> (or IOptionsSnapshot, etc). The linked blog series has a post about configuration that covers some gotchas that I expect I'll eventually run into around caching etc, but it doesn't seem to cater for this scenario of using completely different settings for different tenants. Conceptually it seems simple enough, but I haven't been able to find the correct place to hook in. Please help!
Here is an idea (not tested yet, however). You can save the default IConfiguration instance passed to the constructor of your Startup class and then register in DI your own implementation of IConfiguration that will use that default one and HttpContextAccessor (to get the current tenant).
So the code will look something like:
public class Startup
{
private IConfiguration _defaultConfig;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
_defaultConfig = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
. . . .
services.AddScoped<IConfiguration>(serviceProvider => {
var httpContextAccessor =
serviceProvider.GetService<IHttpContextAccessor>();
return new MyConfig(_defaultConfig, httpContextAccessor);
});
}
. . . .
}
public class MyConfig : IConfiguration
{
private readonly IConfiguration _defaultConfig;
private readonly IHttpContextAccessor _httpContextAccessor;
public MyConfig(IConfiguration defaultConfig, IHttpContextAccessor httpContextAccessor)
{
_defaultConfig = defaultConfig;
_httpContextAccessor = httpContextAccessor;
}
public string this[string key] {
get {
var tenantId = GetTenantId();
return _defaultConfig[tenantId + ":" + key];
}
set {
var tenantId = GetTenantId();
_defaultConfig[tenantId + ":" + key] = value;
}
}
protected virtual string GetTenantId()
{
//this is just an example that supposes that you have "TenantId" claim associated with each user
return _httpContextAccessor.HttpContext.User.FindFirst("TenantId").Value; ;
}
public IEnumerable<IConfigurationSection> GetChildren()
{
return _defaultConfig.GetChildren();
}
public IChangeToken GetReloadToken()
{
return _defaultConfig.GetReloadToken();
}
public IConfigurationSection GetSection(string key)
{
var tenantId = GetTenantId();
return _defaultConfig.GetSection(tenantId + ":" + key);
}
}
Here are 3 solutions that may be helpful. I don't recommend you the IOptionsMonitor<T> because the tenant value is extracted from HttpContext, makes no sense to use the IOptionsMonitor.
Shared code:
public static class Extensions
{
public static string GetTenantName(this HttpContext context)
{
switch (context.Request.Host.Host)
{
case "abc.localhost.com":
return "TenantABC";
case "def.localhost.com":
return "TenantDEF";
default:
throw new IndexOutOfRangeException("Invalid host requested");
}
}
public static MyAppSettings GetAppSettingsByTenant(this IConfiguration config, string tenant)
{
return new MyAppSettings
{
LuckyNumber = int.Parse(config[$"{tenant}:MyAppSettings:LuckyNumber"]),
FavouriteColour = config[$"{tenant}:MyAppSettings:FavouriteColour"]
};
}
}
Solution 1: Scoped MyAppSettings object.
Registration (Startup->ConfigureServices(IServiceCollection)`
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped(sp =>
{
var contextAccessor = sp.GetService<IHttpContextAccessor>();
var config = sp.GetService<IConfiguration>();
return config.GetAppSettingsByTenant(contextAccessor.HttpContext.GetTenantName());
});
...
Usage:
public class TestController : Controller
{
private readonly MyAppSettings _settings;
public TestController(MyAppSettings settings)
{
_settings = settings;
}
[HttpGet]
public IActionResult Index()
{
return Json(_settings);
}
}
Solution 2: IOptions<MyAppSettings
Registration (Startup->ConfigureServices(IServiceCollection)`
public class MyAppSettingsOptions : IOptions<MyAppSettings>
{
public MyAppSettingsOptions(IConfiguration configuration, IHttpContextAccessor contextAccessor)
{
var tenant = contextAccessor.HttpContext.GetTenantName();
Value = configuration.GetAppSettingsByTenant(tenant);
}
public MyAppSettings Value { get; }
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IOptions<MyAppSettings>, MyAppSettingsOptions>();
...
Usage
public class TestController : Controller
{
private readonly IOptions<MyAppSettings> _options;
public TestController(IOptions<MyAppSettings> options)
{
_options = options;
}
[HttpGet]
public IActionResult Index()
{
return Json(_options.Value);
}
}
Solution 3: IOptionsMonitor<MyAppSettings
Registration (Startup->ConfigureServices(IServiceCollection)`
public class MyAppSettingsOptionsMonitor : IOptionsMonitor<MyAppSettings>
{
public MyAppSettingsOptionsMonitor(IConfiguration configuration, IHttpContextAccessor contextAccessor)
{
var tenant = contextAccessor.HttpContext.GetTenantName();
CurrentValue = configuration.GetAppSettingsByTenant(tenant);
}
public MyAppSettings Get(string name)
{
throw new NotSupportedException();
}
public IDisposable OnChange(Action<MyAppSettings, string> listener)
{
return null;
}
public MyAppSettings CurrentValue { get; }
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IOptionsMonitor<MyAppSettings>, MyAppSettingsOptionsMonitor>();
...
Usage
public class TestController : Controller
{
private readonly IOptionsMonitor<MyAppSettings> _options;
public TestController(IOptionsMonitor<MyAppSettings> options)
{
_options = options;
}
[HttpGet]
public IActionResult Index()
{
return Json(_options.CurrentValue);
}
}
You can use DI services to config options
Sample code of your option class
public class MyAppSettings
{
public string FavouriteColor { get; set; }
public int LuckNumber { get; set; }
}
public interface IMySettingServices
{
string GetFavouriteColor();
int GetLuckNumber();
}
public class MySettingServices : IMySettingServices
{
private IHttpContextAccessor httpContextAccessor;
public MySettingServices(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public string GetFavouriteColor()
{
var headers = this.httpContextAccessor.HttpContext.Request.Headers;
//Write your logic with httpContextAccessor by extract tenant here, then return actual config by tenant name
if(this.httpContextAccessor.HttpContext.Request.Host.Host=="abc.test.com")
{
//Get color setting for abc.test.com
}
return "Green";
}
public int GetLuckNumber()
{
var headers = this.httpContextAccessor.HttpContext.Request.Headers;
//Write your logic with httpContextAccessor by extract tenant here, then return actual config by tenant name
if (this.httpContextAccessor.HttpContext.Request.Host.Host == "abc.test.com")
{
//Get luck number setting for abc.test.com
}
return 1;
}
}
Sample code of your ConfigureService
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddControllersWithViews();
services.AddSingleton<IMySettingServices, MySettingServices>();
services.AddOptions<MyAppSettings>().Configure<IMySettingServices>((setting, settingServices) => {
setting.FavouriteColor = settingServices.GetFavouriteColor();
setting.LuckNumber = settingServices.GetLuckNumber();
});//This is the key point of this answer, you are delegating your setting assignment to a services, so you can do whatever you want in your services, in your word, customise configuration binding
}
Sample code of use your configuration in controller
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IOptions<MyAppSettings> settings;
public HomeController(ILogger<HomeController> logger, IOptions<MyAppSettings> settings)
{
_logger = logger;
this.settings = settings;
}
public IActionResult Index()
{
var favColor = settings.Value.FavouriteColor;
return View();
}
}
please be aware that when you want to access httpcontext, do not directly add services.AddScoped/AddSingleton/AddTransit<IHttpContextAccessor,HttpContextAccessor>(), this will result to DI system unable to resolve IHttpContextAccessor during ConfigureServices phases. Use services.AddHttpContextAccessor(); is the best way to do that

Access HttpContextAccessor from Helper Class in .net core web api

I want to access JwtHelper from ExceptionHelper. But problem is ExceptionHelper must be static. And so, we can't create constructor and not access jwtHelper Method. How can I achieve access jwHelper from ExcewptionHelper.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddMvc();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDbContext<MyDbContext>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler(builder => builder.Run(async context =>
{
var error = context.Features.Get<IExceptionHandlerFeature>();
context.Response.AddApplicationError(error);
await context.Response.WriteAsync(error.Error.Message);
}));
app.UseHttpsRedirection();
app.UseMvc();
}
ExceptionHelper.cs
public static class ExceptionHelper
{
public static async Task AddApplicationError(this HttpResponse response)
{
Log log = new Log();
log.UserId = jwtHelper.GetValueFromToken(token, "UserId");??????
//in this line I can't access jwtHelper.
}
}
JwtHelper.cs
public class JwtHelper : IJwtHelper
{
private readonly IHttpContextAccessor httpContextAccessor;
public JwtHelper(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public string GetValueFromToken(string stream, string propertyName)
{
var jwt = httpContextAccessor.HttpContext.Request.Headers["Authorization"];
var handler = new JwtSecurityTokenHandler();
var tokens = handler.ReadToken(stream.Replace("Bearer ", "")) as JwtSecurityToken;
return tokens.Claims.FirstOrDefault(claim => claim.Type == propertyName).Value;
}
}
If I were you I would register JwtHelper with a Interface known as IJwtHelper.
It would look like this then
public class JwtHelper : IJwtHelper
{
private readonly IHttpContextAccessor httpContextAccessor;
public JwtHelper(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public string GetValueFromToken(string propertyName)
{
var jwt= httpContextAccessor.HttpContext.Request.Headers["Authorization"];
// I can't access httpContextAccessor in this line.
var handler = new JwtSecurityTokenHandler();
var tokens = handler.ReadToken(jwt) as JwtSecurityToken;
return tokens.Claims.FirstOrDefault(claim => claim.Type == propertyName).Value;
}
}
public interface IJwtHelper
{
string GetValueFromToken(string propertyName);
}
In my startup.cs class I would then do
services.AddSingleton<IJwtHelper, JwtHelper>();
And then when you want to access your helper I would inject IJwtHelper
private IJwtHelper _jwtHelper;
public SomeConstructerOnClass(IJwtHelper jwtHelper)
{
_jwtHelper = jwtHelper;
}
public void SomeMethod(string property) {
var token = _jwtHelper.GetValueFromToken(property);
//Do something with token
}
where _jwtHelper is field of type IJwtHelper.
You will then be able to use GetValueFromToken quite fine anywhere you inject IJwtHelper
UPDATE
Your problem is that ExceptionHandler is Static , implement an interface and add it to container
public class ExceptionHelper : IExceptionHelper
{
private IJwtHelper _jwtHelper;
public ExceptionHelper(IJwtHelper jwtHelper)
{
_jwtHelper = jwtHelper;
}
public async Task AddApplicationError(this HttpResponse response)
{
Log log = new Log();
log.UserId = _jwtHelper.GetValueFromToken(token, "UserId");??????
}
}
public interface IExceptionHelper
{
Task AddApplicationError( HttpResponse response);
}
Then
services.AddSingleton<IExceptionHelper, ExceptionHelper>();
Now You will be able to inject it into your Configure method like so
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IExceptionHelper exceptionHelper)
{
app.UseExceptionHandler(builder => builder.Run(async context =>
{
var error = context.Features.Get<IExceptionHandlerFeature>();
//Resolved and available!
exceptionHelper.AddApplicationError(error);
await context.Response.WriteAsync(error.Error.Message);
}));
app.UseHttpsRedirection();
app.UseMvc();
}
If you follow me advice above from my initial response and my update everything should be fine and registered nicely in your container :)
You'll have to instantiate the JwtHelper class in order to access the instance variable (httpContextAccessor) from another class. Static methods, like GetValueFromToken, cannot access instance variables.

Dependency Injection in Class Library in ASP.NET Core 2

I have an ASP.NET Core Class Library that depends on dependency injection in the project that references the dll. But when I try to get the service, the service is always null.
This is the only design I have come up so far that should work, since I guess I can't set up dependency services directly in a Class Library.
Class Library:
public interface IHtmlResourceLocalizer
{
string Get(string cultureCode, string name);
string Get(CultureInfo culture, string name);
}
public class HtmlResourceLocalizer : IHtmlResourceLocalizer
{
private readonly IHostingEnvironment _env;
private readonly IHttpContextAccessor _context;
public HtmlResourceLocalizer(IHostingEnvironment env, IHttpContextAccessor context)
{
_env = env;
_context = context;
}
public string Get(string cultureCode, string name) { return Get(new CultureInfo(cultureCode), name); }
public string Get(CultureInfo culture, string name)
{
string result = string.Empty, filename = name + "." + culture.Parent.Name + ".cshtml";
string path = Path.Combine(this.Env.ContentRootPath, "Views", "Resources", filename);
if (File.Exists(path))
{
IRazorViewEngine engine = _context.HttpContext.Features.Get<IRazorViewEngine>();
ITempDataProvider temp = _context.HttpContext.Features.Get<ITempDataProvider>();
ViewRenderUtility util = new ViewRenderUtility(engine, temp, _context.HttpContext.RequestServices);
result = util.ViewToString(filename, null).Result;
}
else
{
throw new FileNotFoundException("Could not find file \"" + path + "\"");
}
return result;
}
public IHostingEnvironment Env { get { return _env; } }
}
Startup in the project that references the Class Library:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddSingleton<IHtmlResourceLocalizer, HtmlResourceLocalizer>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IRazorViewEngine, RazorViewEngine>();
services.AddSingleton<AppHelper>();
services.AddMvc()
.AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
View:
#inject IHtmlResourceLocalizer _local
#Html.Raw(_local.Get("no-NB", "Tech"))

Categories