HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Issue a redirect, trying several string replacements for the domain

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thedomainissuetryingseveralreplacementsredirectforstring

Problem

How could I improve this code?

var to = "http://forum.";

if (!RedirectPermanent("http://www.", to))
    if (!RedirectPermanent("http://blog.", to))
        if (!RedirectPermanent("http://forum.", to))
            if (!RedirectPermanent("http://tracker.", to))
                if (!RedirectPermanent("http://wiki.", to))
                    RedirectPermanent("http://", to);


This is the method:

private bool RedirectPermanent(string from,string to)
{
    if (Request.Url.AbsoluteUri.StartsWith(from))
    {
        var domain = Request.Url.GetLeftPart(UriPartial.Authority).Replace(from, to);
        Response.RedirectPermanent(string.Concat(domain, Request.RawUrl));
        return true;
    }

    return false;
}

Solution

Went for this:

var arr = new[] { "http://www.", "http://blog.", "http://forum.", "http://tracker.", "http://wiki.", "http://" };
var to = "http://forum.";

foreach (var from in arr)
{
    if (RedirectPermanent(from, to))
        break;
}

Code Snippets

var arr = new[] { "http://www.", "http://blog.", "http://forum.", "http://tracker.", "http://wiki.", "http://" };
var to = "http://forum.";

foreach (var from in arr)
{
    if (RedirectPermanent(from, to))
        break;
}

Context

StackExchange Code Review Q#4087, answer score: 5

Revisions (0)

No revisions yet.