اشتراکها
اشتراکها
کتابخانهی JSONAPI.NET
JSONAPI.NET is a set of utility classes that aim to make it possible to implement JSON API spec compliant RESTful web services quickly and easily using ASP.NET MVC WebAPI.
//... .AddOpenIdConnect("oidc", options => { // ... options.Events = new OpenIdConnectEvents { OnTokenValidated = async ctx => { // how to access claims var user = ctx.Principal; var email = user.Claims.FirstOrDefault(claim => claim.Type == "email").Value; // how to access services var db = ctx.HttpContext.RequestServices.GetRequiredService<MyDb>(); // .... } }; });
برای کامپایل مجدد فایلهای ویو (cshtml) در هنگام اجرای برنامه (runtime compilation) و مشاهده تغییرات اعمال شده بر روی آنها به صورت زیر عمل میکنیم:
- ASP.NET Core 2.2
services.AddMvc() .AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
- ASP.NET Core 3.0 , 3.1
ابتدا بسته Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation به پروژه اضافه کرده و سپس از کد زیر استفاده میکنیم:
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews() .AddRazorRuntimeCompilation(); //... }
public async Task<HttpClient> GetHttpClientAsync() { var currentContext = _httpContextAccessor.HttpContext; // ... }
public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor();
در متد configure در startup سرویس خودم رو به این شکل فراخوانی کردم
var df = languageService.GetAll().GetAwaiter().GetResult();
یک نکتهی تکمیلی: تعریف متد AddConfig و عدم نیاز به استفادهی از IOptions برای کار با آن
کدهای متد الحاقی AddConfig زیر:
services.AddConfig<MySettings>(Configuration.GetSection("MySettings"));
public static class ServiceCollectionExtensions { public static TSettings AddConfig<TSettings>(this IServiceCollection services, IConfiguration configuration) where TSettings : class, new() { return services.AddConfig<TSettings>(configuration, options => { }); } public static TSettings AddConfig<TSettings>(this IServiceCollection services, IConfiguration configuration, Action<BinderOptions> configureOptions) where TSettings : class, new() { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } TSettings setting = configuration.Get<TSettings>(configureOptions); services.TryAddSingleton(setting); return setting; } }
private readonly MySettings _settings; public MyViewComponent(MySettings settings) { _settings = settings; }
نظرات مطالب
حذف فضاهای خالی در خروجی صفحات ASP.NET MVC
جهت فشرده سازی چون gzip در net core. میتوان از این middleware به شکل زیر استفاده کرد:
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddResponseCompression(); services.AddResponseCompression(options => { options.Providers.Add<GzipCompressionProvider>(); options.EnableForHttps = true; }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseResponseCompression(); } }