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). 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, penulis sekaligus pecinta sastra, dan pembaca buku

Halo, perkenalkan saya Agung Setiawan.
Seorang Software Engineer di BukaLapak, penulis buku, blogger, gunpla builder, tech educator.
Simak pemikiran saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
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 *