ASP.NET MVC Redirect on Filter

Here’s the situation that you might encounter, you write your own custom filter to meet your need, for instance to perform checking whether a user is login on the system or not by checking the session, and if not then the user must be redirected to somewhere else safe(you decide).

asp.net mvc redirect on filter


In this blog post i will show you how to do redirection on filter instead inside controller’s action method.

Before i go to the main topic let’s remind how to perform redirection on action method.

At the first time you thought that the above methods will also work on filter, but they don’t, and you end up come here read my blog, thank you for stop by πŸ™‚

So without too much talk i present you sample code that demonstrate how to redirect from filter.

On the code above the user will be redirected to “/login” when he/she is not login. What if you want to use an action name and controller name and even route value as the target of redirection? As easy as following example.

Hope this solve your problem πŸ˜‰

Facebook Comments
 

Agung Setiawan

Agung Setiawan adalah software engineer di BukaLapak.com, penulis sekaligus pecinta sastra, dan pembaca buku

 
Halo, perkenalkan saya Agung Setiawan.
Saya Software Engineer di BukaLapak.
Simak pemikian saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Blog ini saya update seminggu sekali jadi sering-sering saja mampir
Mau belajar Vim bareng saya?
Belajar ngoding dari nol menggunakan PHP

Leave a Reply

Your email address will not be published. Required fields are marked *