小编Meh*_*hdi的帖子

如何在ASP.net core中手动取消BackgroundService

我创建一个像这样的BackgroundService:

public class CustomService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken cancellationToken)
    {
        do
        {
            //...

            await Task.Delay(60000, cancellationToken);
        }
        while (!cancellationToken.IsCancellationRequested);
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么手动取消呢?

c# background-service cancellation-token asp.net-core

15
推荐指数
1
解决办法
2万
查看次数

将 ASP.NET Core Razor 页面中的默认页面从 Index 更改为 Home

我正在更改默认页面

services.AddRazorPages(options =>
{
    //...
}).AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Home", "");
});
Run Code Online (Sandbox Code Playgroud)

但发生了异常

AmbiguousMatchException: The request matched multiple endpoints. Matches:
/Home
/Index
Run Code Online (Sandbox Code Playgroud)

我能做些什么?

c# asp.net-core razor-pages

8
推荐指数
1
解决办法
1万
查看次数

ResponseCache 在 ASP.net core 中始终将 Cache-Control 设置为 no-cache、no-store

我想在客户端浏览器中缓存主页

但设置ResponseCache属性后,响应头设置为Cache-Control: no-cache, no-store

阅读Response cache not working in asp.net core projectResponseCache notworking in net core 3.1后,我无法修复此问题

[ResponseCache(Duration = 300)]
public class HomeModel : PageModel
{

    public const string PageName = "Home";

    public void OnGet()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

缓存控制

c# asp.net-core

4
推荐指数
1
解决办法
2459
查看次数

EF core 中的 DbGeography 相当于什么?

EF core 中的 DbGeography 相当于什么?
我想将以下代码从 EF 转换为 EF Core:

using System.Data.Entity.Spatial;

namespace Domain
{
    public class City
    {
        public int CityId { get; set; }
        
        public DbGeography Coordinates { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# spatial entity-framework-core .net-core

3
推荐指数
1
解决办法
3944
查看次数

如果我们不等待异步 javascript 函数怎么办?

如果我们不等待异步 javascript 函数怎么办?

据我所知,像 C# 这样的一些语言不应该运行非托管的异步函数!

我想知道 JavaScript 语言是否也是如此?

var asynchronousFunction = async function() {
    //...
}

function main() {
   var result = true;
   //...
   asynchronousFunction(); // The result of this function has no effect on our output (result)
   //...
   return result;
}
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous async-await

3
推荐指数
1
解决办法
1820
查看次数

Asp.net core:如何通过404状态码获取错误页面中的原始路径

在我的asp.net core应用程序中,每次发生错误后,请求的地址都会更改为站点错误页面地址,如何在错误页面中检索原始请求的地址?
启动.cs:

public class Startup
{
    private const string ErrorHandlingPath = "/Error";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseExceptionHandler(ErrorHandlingPath);

        app.UseStatusCodePagesWithReExecute(ErrorHandlingPath);;

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

错误.cshtml.cs


    public class ErrorModel : PageModel
    {
        public void OnGet()
        {
            var status = HttpContext.Response.StatusCode;//=404
            var originalPath = HttpContext.Request.Path.Value;//=Error
            var feauter = Request.HttpContext.Features.Get<IExceptionHandlerPathFeature>();//=null
            var path = feauter?.Path;//=null
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

2
推荐指数
1
解决办法
1281
查看次数

ASP.net core:将页面渲染为字符串期间模型为空

我想将页面呈现为电子邮件模板。
但是当渲染器想要使用我遵循本教程的模型时,我收到错误
learnrazorpages.com

RazorRenderer.cs:

public class RazorRenderer : IRazorRenderer
{
    private readonly IRazorViewEngine _viewEngine;
    private readonly ITempDataProvider _tempDataProvider;
    private readonly IServiceProvider _serviceProvider;

    public RazorRenderer(
        IRazorViewEngine viewEngine,
        ITempDataProvider tempDataProvider,
        IServiceProvider serviceProvider)
    {
        _viewEngine = viewEngine;
        _tempDataProvider = tempDataProvider;
        _serviceProvider = serviceProvider;
    }

    public async Task<string> ToStringAsync<TModel>(string partialName, TModel model)
    {
        var actionContext = GetActionContext();
        var partial = FindView(actionContext, partialName);
        var viewData = new ViewDataDictionary<TModel>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
        var tempData = new TempDataDictionary(actionContext.HttpContext, _tempDataProvider);

        viewData.Model = model;

        await using var stringWriter …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core razor-pages

2
推荐指数
1
解决办法
1388
查看次数