Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 混合多个请求的基本.NET核心应用程序_C#_.net_Asp.net Core - Fatal编程技术网

C# 混合多个请求的基本.NET核心应用程序

C# 混合多个请求的基本.NET核心应用程序,c#,.net,asp.net-core,C#,.net,Asp.net Core,我正在使用.NET 5/Core并创建了最基本的ASP.NET Core web应用程序来理解.NET Core基础知识(下面是我的代码),但我的私有类字段/属性的行为类似于静态字段,并在HTTP请求之间保留其值。我想了解为什么,如何以最简单的方式解决这个问题,以及是否有多种解决方案/方法 我将“Output”字符串声明为私有非静态字段,并假设它应该随着每个新的HTTP请求而重置,但它的行为与静态字段/属性类似,每次新页面刷新都会导致越来越长的输出,保留以前的值并添加一个新值。 例如,网页首先显

我正在使用.NET 5/Core并创建了最基本的ASP.NET Core web应用程序来理解.NET Core基础知识(下面是我的代码),但我的私有类字段/属性的行为类似于静态字段,并在HTTP请求之间保留其值。我想了解为什么,如何以最简单的方式解决这个问题,以及是否有多种解决方案/方法

我将“Output”字符串声明为私有非静态字段,并假设它应该随着每个新的HTTP请求而重置,但它的行为与静态字段/属性类似,每次新页面刷新都会导致越来越长的输出,保留以前的值并添加一个新值。 例如,网页首先显示输出
/
,但在刷新页面几次后,我看到以下5个输出合并

/
/favicon.ico
/
/favicon.ico
/
favicon.ico
不是问题,我知道我的网络浏览器正在请求它。上面的输出在前3次页面刷新后显示,而由于web浏览器提交了2个HTTP请求,因此每次额外的页面刷新都会输出其中的两行。 我可能可以使用
services.AddScoped
services.AddTransient
想出一个解决方案,但不确定在这种情况下为什么需要服务,因为每个HTTP请求似乎都是单独处理的,应该重新初始化我的RequestHandler类。我是否遗漏了一些简单的东西,是否有一种方法可以在示例代码的简单性级别处理这些问题

我的整个应用程序由以下Startup.cs组成:

using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

namespace BasicApp
{
    public class Startup
    {
        public static void Main()
        {
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMiddleware<RequestHandler>();
        }
    }

    public class RequestHandler
    {
        RequestDelegate _next;
        private string Output = "";

        public RequestHandler(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            Output += context.Request.Path + "\n";
            await context.Response.WriteAsync(Output);
        }
    }
}
使用System.Threading.Tasks;
使用Microsoft.AspNet.Builder;
使用Microsoft.AspNet.Http;
名称空间基CAPP
{
公营创业
{
公共静态void Main()
{
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseMiddleware();
}
}
公共类RequestHandler
{
请求委托(下一步);
私有字符串输出=”;
公共RequestHandler(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
输出+=context.Request.Path+“\n”;
wait context.Response.WriteAsync(输出);
}
}
}

中间件类仅通过
iaapplicationbuilder.UseMidleware{T}
实例化一次,并放入请求管道。这本书在解释过程方面做得相当好

您可以通过
iaapplicationbuilder.Run
方法实现简单的响应:

public void Configure(IApplicationBuilder app)
{
    app.Run(async context =>
    {
        await context.Response.WriteAsync(context.Request.Path);
    });
}
另一种考虑方法是,中间件类用每个请求实例化您的
RequestHandler

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMiddleware<RequestHandlerMiddleware>();
    }

    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

public class RequestHandlerMiddleware
{
    RequestDelegate _next;

    public RequestHandlerMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var handler = new RequestHandler();
        await handler.HandleRequestAsync(context);
        await _next.Invoke(context); // invoke the next delegate in the pipeline
    }
}

public class RequestHandler
{
    private string Output = "";

    public async Task HandleRequestAsync(HttpContext context)
    {
        Output += context.Request.Path + "\n";
        await context.Response.WriteAsync(Output);
    }
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseMiddleware();
}
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
}
公共类RequestHandler中间件
{
请求委托(下一步);
公共RequestHandlerMiddleware(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
var handler=newrequesthandler();
wait handler.HandleRequestAsync(上下文);
wait _next.Invoke(context);//调用管道中的下一个委托
}
}
公共类RequestHandler
{
私有字符串输出=”;
公共异步任务HandleRequestAsync(HttpContext上下文)
{
输出+=context.Request.Path+“\n”;
wait context.Response.WriteAsync(输出);
}
}

中间件类仅通过
iaapplicationbuilder.UseMidleware{T}
实例化一次,并放入请求管道。这本书在解释过程方面做得相当好

您可以通过
iaapplicationbuilder.Run
方法实现简单的响应:

public void Configure(IApplicationBuilder app)
{
    app.Run(async context =>
    {
        await context.Response.WriteAsync(context.Request.Path);
    });
}
另一种考虑方法是,中间件类用每个请求实例化您的
RequestHandler

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMiddleware<RequestHandlerMiddleware>();
    }

    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

public class RequestHandlerMiddleware
{
    RequestDelegate _next;

    public RequestHandlerMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var handler = new RequestHandler();
        await handler.HandleRequestAsync(context);
        await _next.Invoke(context); // invoke the next delegate in the pipeline
    }
}

public class RequestHandler
{
    private string Output = "";

    public async Task HandleRequestAsync(HttpContext context)
    {
        Output += context.Request.Path + "\n";
        await context.Response.WriteAsync(Output);
    }
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseMiddleware();
}
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
}
公共类RequestHandler中间件
{
请求委托(下一步);
公共RequestHandlerMiddleware(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
var handler=newrequesthandler();
wait handler.HandleRequestAsync(上下文);
wait _next.Invoke(context);//调用管道中的下一个委托
}
}
公共类RequestHandler
{
私有字符串输出=”;
公共异步任务HandleRequestAsync(HttpContext上下文)
{
输出+=context.Request.Path+“\n”;
wait context.Response.WriteAsync(输出);
}
}

你的问题是什么?我不确定您希望在这里发生什么行为。我希望私有输出字段/属性的行为不会像静态字段一样。因此,它应该被初始化,并为每个新的HTTP请求重置其值。我没有将其声明为静态,但它在HTTP请求之间保留其值,就像它是静态的一样。我的意思是,你不能依赖一个中间件,它不会被初始化,也不会为每个新的HTTP请求重置它的值。只是想了解如何以最基本的方式为每个HTTP请求自动初始化它。你的问题是什么?我不确定您希望在这里发生什么行为。我希望私有输出字段/属性的行为不会像静态字段一样。因此,它应该被初始化,并为每个新的HTTP请求重置其值。我没有将其声明为静态,但它在HTTP请求之间保留其值,就像它是静态的一样。我