Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created January 30, 2012 14:46
Show Gist options
  • Save ToJans/1704780 to your computer and use it in GitHub Desktop.
Save ToJans/1704780 to your computer and use it in GitHub Desktop.
NancyFX extension method to get remapped url
return Response.AsRedirect(request.Url.Remap(scheme="https").ToString());
return Response.AsRedirect(request.Url.Remap("~/Retry/").WithQueryParameter("Message","Invalid blah").ToString());
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nancy;
using System.Net;
using System.Net.Sockets;
namespace Blommekes.Infrastructure
{
public static class NancyExtensions
{
/// <summary>
/// Remaps a a url
/// Replacing a parameter with null keeps the old value intact
/// Replacing it with an empty string "" removes it
/// </summary>
/// <param name="input">The original url to base your new one on
/// </param>
/// <param name="path">The path to base your new one on
/// * null => keep the original path intact
/// * starts with "~/"+[xxx] resolves to base path+[xxx]
/// * starts with "/"+[xxx] resolves to root path+[xxx]
/// * others map path relative to current path
/// </param>
/// <param name="Query">Query to use; null=keep the original one</param>
/// <param name="fragment">Fragment to use; null=keep the original one</param>
/// <param name="Scheme">Scheme (i.e. http or https) to use; null=keep the original one</param>
/// <param name="Hostname">Hostname to use; null=keep the original one</param>
/// <param name="Port">Port to use; null=keep the original one</param>
/// <returns></returns>
public static Url Remap(this Nancy.Url input, string path = null, string query = "", string fragment = "", string scheme = null, string hostname = null, int? port = null)
{
var url = new UrlFromV10(); // did this because I'm using nuget/old version
url.Scheme = scheme ?? input.Scheme;
url.HostName = hostname ?? input.HostName;
url.Port = port ?? input.Port;
if (path == null)
url.Path = input.Path;
else
{
if (path.StartsWith("~/"))
{
url.Path = path.Substring(1);
}
else if (path.StartsWith("/"))
{
url.Path = path;
url.BasePath = null;
}
else
{
path = "/" + path;
if (input.Path == null)
url.Path = path;
else
{
if (!input.Path.EndsWith("/"))
input.Path += "/";
url.Path = input.Path + path;
}
}
}
url.Query = query ?? input.Query;
url.Fragment = fragment ?? input.Fragment;
return url;
}
// Untested
public static Url WithQueryParameter(this Url url, string name, string value)
{
var str = name + "=" + Uri.EscapeDataString(value);
if (!string.IsNullOrEmpty(url.Query))
url.Query += "&";
url.Query = str;
return url;
}
}
public class UrlFromV10 : Url
{
public override string ToString()
{
return this.Scheme + "://" +
GetHostName(this.HostName) +
GetPort(this.Port) +
GetCorrectPath(this.BasePath) +
GetCorrectPath(this.Path) +
this.Query +
GetFragment(this.Fragment);
}
private static string GetFragment(string fragment)
{
return (string.IsNullOrEmpty(fragment)) ? string.Empty : string.Concat("#", fragment);
}
private static string GetCorrectPath(string path)
{
return (string.IsNullOrEmpty(path) || path.Equals("/")) ? string.Empty : path;
}
private static string GetPort(int? port)
{
return (!port.HasValue) ?
string.Empty :
string.Concat(":", port.Value);
}
private static string GetHostName(string hostName)
{
IPAddress address;
if (IPAddress.TryParse(hostName, out address))
{
return (address.AddressFamily == AddressFamily.InterNetworkV6)
? string.Concat("[", address.ToString(), "]")
: address.ToString();
}
return hostName;
}
}
}
@ToJans
Copy link
Author

ToJans commented Jan 30, 2012

The module is still under dev, so I know there are some copy/paste errors in there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment