Question: My middleware class is in different class library project and controller is in different project. What I am trying to do, if specific condition does not meet then redirect to the Custom Controller/Action method from middleware.

However, I am not able to do that with Response.Redirect method.

How can I do this in middleware class ?

Any help on this appreciated !

Rohit

Answer: 
Here is middleware that examines the request and redirects. It works with either inline middleware or with a middleware class.

```
public void Configure(IApplicationBuilder app)
{
    // use inline middleware
    app.Use(async (context, next) =>
    {
        // if specific condition does not meet
        if (context.Request.Path.ToString().Equals("/foo"))
        {
            context.Response.Redirect("path/to/controller/action");
        }
        else
        {
            await next.Invoke();
        }
    });

    // or use a middleware class
    app.UseMiddleware<RedirectMiddleware>();

    app.UseMvc();
}

```

Here is the middleware class.

```
public class RedirectMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task Invoke(HttpContext context)
    {
        // if specific condition does not meet
        if (context.Request.Path.ToString().Equals("/bar"))
        {
            context.Response.Redirect("path/to/controller/action");
        }
        else
        {
            await _next.Invoke(context);
        }
    }
}

```

See [Docs » Fundamentals » Middleware](https://docs.asp.net/en/latest/fundamentals/middleware.html#writing-middleware) for more info.
You can use "Request Editing Middleware" to change the nature of the request as it moves down the pipeline. This fits in with the general "best practices" and will be more efficient than sending a redirect notice back to the browser.

This example redirects to an action method than can return an image:

```
public class ResolveImageMiddleware
{
    private readonly RequestDelegate _next;

    public ResolveImageMiddleware(RequestDelegate deg)
    {
        _next = deg;
    }

    public async Task InvokeAsync(HttpContext context, AppDbContext db)
    {
        var path = context.Request.Path;
        if (path.HasValue && path.Value.StartsWith("/imgs"))
        {
            context.Request.Path = "/Home/GetImageFromDb";
            context.Request.QueryString = new QueryString("?title=" + path.Value.Replace("/imgs/", ""));
        }

        await _next(context);
    }

}

```

And create an action method that takes a query and can return a file:

```
public class HomeController : Controller
{
    private EFImageRepo imageRepository;
    public HomeController(EFImageRepo repo)
    {
        imageRepository = repo;
    }
    public FileResult GetImageFromDb([FromQuery]string title)
    {
        var img = imageRepository.GetImg(title);
        if (img != null)
        {
            byte[] contents = img.ImageBytes;
            return File(contents, "image/jpg");
        }
        return null;
    }
}

```

If you use a redirect on response, it will cause the user's browser to receive a redirect notice and then send a new request to your application, rather than consuming the original request and spitting out the file right away.