c# - Call controller's action method from middleware -


my middleware class in different class library project , controller in different project. trying do, if specific condition not meet redirect custom controller/action method middleware.

however, not able response.redirect method.

how can in middleware class ?

any on appreciated !

rohit

it seems you're using middleware wrong reasons.

i recommend either have middleware return (very minimal) 404 writing response stream (instead of forwarding next()), or don't in middleware @ instead in globally registered iactionfilter in mvc app.


i've explained rationale above advice in comments, think it's important enough lift actual answer:

in middleware pipeline, want each component independent possible. couple of things enable loose coupling in owin:

  • the input to, , output from, each component has same format, whether there 10 other middleware components before it, or none @ all

  • the convention each part of pipeline can 1 or more of 3 things, in order:

    1. read (and modify) incoming request.

    2. decide handle request entirely, or forward handling next component.

    3. write response stream.

when sticking these conventions, becomes easy compose, decompose , re-compose pipelines reusable middleware components. (want request logging? hook middleware component @ start of pipe. want general authentication logic across board? add component in auth stage of pipe. want switch different logging framework? replace logging component. want apply same logging across ecosystem of microservices? re-use component. etcetera, ad infinum...) works well, because components both stay within boundaries, , work contract web server can understand.

asp.net webapi might seem different beast, in reality it's owin component configured handle request, , never forward next component (and they've made difficult register component after webapi in pipeline...).

what you're trying do, breaks contract of second point there - want tell next component how handle request. but that's not - it's next component.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -