Home > Mobile >  ASP.NET Core middleware redirect to another route
ASP.NET Core middleware redirect to another route

Time:02-05

I have an ASP.NET 6 Web API controller and it has two methods.

This is my code:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    [HttpPost]
    [Route("Test")]
    public IEnumerable<WeatherForecast> Test()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

    [HttpGet]
    [HttpPost]
    [Route("User")]
    public IEnumerable<WeatherForecast> UserInfo()
    {
        return Enumerable.Range(1, 2).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

Then I wrote a middleware component:

public class RequestResponseMiddleware
{
    private RequestDelegate _next;

    public RequestResponseMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var req = context.Request;
        context.Response.Redirect("/weatherforecast/User",true,true);
         await _next(context);
     }
}

I used

https://localhost:7290/weatherforecast/Test?t=1675343945003&m=&k=sdf

to test it.

In the middleware, response can redirect to /weatherforecast/User, but it will fire 5 times. It means that the method UserInfo() will be invoked 5 times.

What's wrong with my code? And how to fix the problem?

----update----

mapping example.

{
    "Routes":[
        {
            "Controllor": "WeatherForecast",
            "Metods":[
                {
                    "Test": "User" 
                },
                {
                    "Weather": "Weather"
                }
            ]
        }
    ]
}

---- update2 ----

context.Response.Redirect will invoke WeatherForecast/ Test1 time, and invoke WeatherForecast/User4 times.

---- update 3 ----

code enter image description here

CodePudding user response:

i know what's wrong with my code . There is not a exist condition before redirect. a solution likes

            if (context.Request.Path.HasValue && context.Request.Path == "/weatherforecast/Test") 
            {
                context.Response.Redirect("/weatherforecast/User", false);
            }
            else
            {
                await _next.Invoke(context);
            }
  • Related