PolarisPowerShell

Polaris tip for redirecting route to different websites or to other route of same web app.

 

Redirecting app route is quite common requirement in the world of web app. So, you can try the same with PowerShell Polaris module for your best fit scenarios. Requirement is to redirect end users to http://localhost:8080/{thispage} landing page when they try accessing the page http://localhost:8080/{thatpage}NOTE: Both routes are in same web app.

For this demo I am using the repo downloaded from the GitHub – The one published over the PS Gallery has no StatusCode property or some reason it failed for me.

Very simple and straight forward approach using Polaris!

Reference

HttpListenerResponse.Redirect(String) Method

HttpListenerResponse.RedirectLocation Property

Clone the Polaris repo from GitHub and spin up your PowerShell – I tried the version in the PSGallery in which StatusCode property was missing. So, it fails.

Using REDIRECT METHOD (Internal Route – Same Web App)
Import-Module C:\Projects\Polaris\Polaris.psd1 -Verbose
New-PolarisGetRoute -Path "/thispage" -Scriptblock {
    $Response.RawResponse.StatusCode = [int]([System.Net.HttpStatusCode]::Redirect)
    # USING REDIRECT METHOD
    $Response.RawResponse.Redirect("http://localhost:8080/thatpage")
    $Response.RawResponse.Close()
}

New-PolarisGetRoute -Path "/thatpage" -Scriptblock {
    $Response.SetContentType('text/html')
    $Response.Send("<b>You are redirected by Polaris Route &hearts;!</b>")
}

Start-Polaris -Port 8080
Using REDIRECT LOCATION PROPERTY (Other Websites)
Import-Module C:\Projects\Polaris\Polaris.psd1 -Verbose
New-PolarisGetRoute -Path "/Redirect" -Scriptblock {
    $Response.RawResponse.StatusCode = 301
    $Response.RawResponse.RedirectLocation = ("http://www.microsoft.com")
    $Response.RawResponse.Close()
}

Start-Polaris -Port 8080

Follow me on @chendrayanv for more Polaris updates!

Leave a Reply

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