【c# – ASP.NET Core MVC中使用Prometheus的示例项目?】教程文章相关的互联网学习教程文章

c# – 获取ASP.NET Core 1中的所有缓存【代码】

版本是rc1.在我的旧项目中,有类似的代码System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache; System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator(); while (cacheEnumerator.MoveNext()) {....}在核心1中我使用IMemoryCache,我可以通过键获得缓存var c = this._memoryCache; var data = c.Get("data");我想创建一个列出所有缓存的视图.如何在Core 1中获取所有缓存?最佳答案:查看github上ASP.N...

c#-将cookie设置为Asp.Net Core TestServer的httpClient【代码】

我正在使用TestServer测试ASP.NET Core应用,并且有些控制器需要Cookie身份验证.我已经创建了这样的测试服务器实例:_testServer = new TestServer(new WebHostBuilder().UseEnvironment(CustomEnvironments.Test).UseContentRoot(currentDirectory) .UseStartup<Web.Startup>().UseUrls("http://localhost/"));ApiClient = _testServer.CreateClient();现在我必须添加身份验证Cookie,但服务器会忽略它.如果可以直接...

首页> C#> ASP.NET Core如何能够将任何类型转换为ActionResult返回类型的控制器操作?【代码】

我在ASP.NET Core 2.2中的WebApi控制器中有一个简单的操作,如下所示:[HttpGet("test123")] public ActionResult<string> Test123() {return new OkResult(); }这样编译可以,但是我想知道OkResult对象怎么可能转换为ActionResult< string&gt ;?这些类具有不同的继承链:OkResult-> StatusCodeResult->动作结果而ActionResult< TValue>仅实现IConvertToActionResult换句话说,ActionResult< string>不是OkResult类的基本类型. 如果我...

C#编写一个在asp.net core 3.1下的简单的corn模式的计划任务和一个更简单的定时器类【代码】

asp.net core 下,新增了一个BackgroundService用来实现能在后台跑一个长久运行的任务,因此,也可以用来替换掉原来使用的static的Timer组件, Timer组件主要有以下几个麻烦的地方 1.如果是需要长时间跑的定时任务,需要定义为static,,在asp.net core下,无法利用到DI,无法从DI中获取DbContext之类的 2.启动定时器的时候,需要在start.cs自己手动启动 3.Timer是传入处理函数的方式,如果有好几个定时器,拼在一起,代码会看起来比较乱 4.使用...

c# - 在ASP.NET Core上检测与SSE的断开连接 - 编程笔记【代码】

原文:c# - 在ASP.NET Core上检测与SSE的断开连接 - 编程笔记c# - 在ASP.NET Core上检测与SSE的断开连接 Detecting a disconnection from SSE on ASP.NET Core ? I want to detect a disconnection from SSE (Server Send Events) on ASP.NET Core. Old way Response.IsClientConnected not working. Do you have solution for this problem?? Answer ?I found temporary solution to my question. Temporary because IsCancellati...

Asp.Net Core Grpc使用C#对象取代Proto定义【代码】

Asp.Net Core 3.0之后,对Grpc提供了高集成度的支持,对于需要连续传输大批量对象数据的应用场景而言,等于多了一条高铁线路。如果没有Grpc,连续传输大批量对象数据是一个很纠结的问题。用TCP的话,可以达到最高速度,但是传输过程的断线续传,对象数据的序列化和反序列化都要自己处理,开发效率低效。用HTTP的话,要频繁调用POST,反复建立连接,传输性能差。Grpc能够一次建立传输通道,多次传输对象数据,自动序列化和反序列化,...

c# - Asp.net Core Identity - 操作步骤

1. Define a new class, derived from IdentityUserpublic class AppUser : IdentityUser ? ? { ? ? ? ? public string DisplayName { get; set; } ? ? } 2. In your DataContext, change public class DataContext : DbContext to public class DataContext : IdentityDbContext<AppUser> 3. In your DataContext, method: protected override void OnModelCreating(ModelBuilder builder) add code: base.OnModelCreating(bui...

c#-使用Microsoft Graph令牌通过Jwt Bearer令牌保护ASP.NET Core Web API【代码】

我们有一个我想使用Microsoft Graph Access令牌保护的ASP.NET Core Web API.图令牌是有效的,我可以做图调用,它工作正常. 但是,如果尝试访问配置了JWT Bearer身份验证的ASP.NET Core Web API,则会出现以下错误.Bearer error=”invalid_token”, error_description=”The signature keywas not found我是否缺少一些要配置的配置,或者这是图形令牌的问题?这是配置身份验证的方式.services.AddAuthentication(JwtBearerDefaults.Authe...

C# .net 提升 asp.net mvc, asp.net core mvc 并发量【代码】

1.提升System.Net.ServicePointManager.DefaultConnectionLimit 2.提升最小工作线程数 使用ThreadPool.GetMinThreads(out workerThreads, out completePortsThreads); 方法可以得到当前“最小工作线程数”和“最小IO工作线程数”,这两个值默认等于CPU核心数,我这里等于6. 显然,这点并发处理能力是不够的。需要提升,但又不能超过“最大工作线程数”除以2,超过后,又被还原成默认值了。 方式一,在代码中加大(.net mvc 在Appli...

c#-EmailAttribute在ASP.NET Core的ViewModel中无法正确验证【代码】

这是我的视图模型中的一个字段:[Required(ErrorMessage = "Email is missing."), EmailAddress(ErrorMessage = "Email is not valid.")] public string Email { get; set; }(EmailAddress来自EmailAddressAttribute.EmailAddressAttribute()类型)这是HTML中的相关部分:<div><label for="inputEmail">EMAIL</label><input id="inputEmail" ng-model="email" asp-for="Email" class="form-control" /> </div> <div><span asp-valid...

c#-如何在ASP.NET Core 1.1中对使用HttpContext的MVC控制器进行单元测试【代码】

我在.Net Core 1.1框架上用ASP.Net Core编写了这个小方法:public class AccountController : Controller {public IActionResult Logout(){HttpContext.Authentication.SignOutAsync("SchemaName");HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);return RedirectToAction("Index", "Home");} }我在如何编写验证此方法返回RedirectToActionResult的单元测试方面苦苦挣扎,并根据在此...

c#-在IIS 7中托管Asp.Net Core【代码】

我有一台较旧的服务器,已安装了所有最新的Internet信息系统(IIS)核心Web托管模块.服务器应该是最新的. 由于某些奇怪的原因,我收到以下错误:HTTP Error 500.19 – Internal Server Error The configuration section‘aspNetCore’ cannot be read because it is missing a sectiondeclaration.web.config如下:<?xml version="1.0" encoding="utf-8"> <configuration><system.webServer><handlers><add name="aspNetCore path="*" ...

c#-创建方法以返回Serilog LoggerConfiguration(Asp.Net Core 2.0)【代码】

最初,我为Serilog使用了一个日志文件,通过完成此操作var slc = new SerilogSubLoggerConfiguration(); configuration.GetSection("Serilog:SubLogger").Bind(slc);然后在Program.cs的Main方法中配置SubLoggerLog.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).WriteTo.Logger(logger => logger.Filter.ByIncludingOnly(lvl => lvl.Level == slc.Level).WriteTo.RollingFile(slc..PathFormat)).Cre...

c#-从ASP.NET Core Web App访问类库中的实体框架【代码】

目前,我的解决方案中有2个项目,一个ASP.NET Core Web应用程序和一个.NET 4.6.1(非核心)类库.在我的类库中,我添加了Entity Framework 6,并使用数据库第一代创建了所有数据库上下文类.在类库中,一切都按预期工作;但是,当我尝试从.NET Core Web App访问ef类时,它没有要使用的连接字符串,并且会失败. 现在,以旧的方式,我知道可以将连接字符串设置从类库中的app.config复制到Web应用程序中的web.config,但是.NET Core不支持此设置. 我已...

c#-数字格式在ASP.NET Core中不起作用【代码】

我尝试在我的应用程序(Azure中的.Net 4.5.2 Full Framework上运行的ASP.NET Core)中允许使用小数.该应用程序被配置为仅在Startup.cs中使用自定义DateTime格式的de-DE文化:var dtf = new DateTimeFormatInfo {ShortDatePattern = "dd.MM.yyyy",LongDatePattern = "dd.MM.yyyy HH:mm",ShortTimePattern = "HH:mm",LongTimePattern = "HH:mm" };services.Configure<RequestLocalizationOptions>(options => {var supportedCultures =...