Custom Redirect Results in Asp.Net MVC


Posted by admin on September 01, 2019

So after writing lots of code with return new redirect(“”) or RedirectToRoute etc, I decided i had had enough, and thought it would be a little nicer to have something return that made sense and could be reused, instead of stings for locations all over the place making my code look nasty.

So here is the the problem code that has lots of things that look like this…

return Redirect("/paymentoffline");

So instead of having this code smell, why not replace it with a class, somewhere we only have to specify the url once?

public class PaymentOfflineResult : RedirectResult {
    public PaymentOfflineResult()
        : base("/paymentoffline") {

And now all we have to do is….

return new PaymentOfflineResult();

Is that not a lot cleaner??