Search code examples
iis-7.5nancyvirtual-directory

How do you tell Nancy to redirect to a URL inside a virtual directory?


I'm running a Nancy application in a Virtual Directory on my development machine and I've got static content working but if the module redirects the browser then the URL being sent back is incorrect.

If the result is:

return new RedirectResponse("/");

Then the browser redirects to http://localhost/ rather than the root of the virtual directory.

If I try

return new RedirectResponse("~/");

I get taken to http://localhost/MyVirtualDirectory/action/~/ which at least includes the virtual directory, but the rest is screwed up.

I should point out that the module is created like this...

public abstract class ActionRootModule : NancyModule
{
    protected ActionRootModule() : base("/action/") { }
}

public class SendEmailModule : ActionRootModule
{
    public SendEmailModule()
    {
        // Parts missing for brevity....
        Post["/send-email/"] = o => PostSendEmail(o);
    }

    private dynamic PostSendEmail(dynamic o)
    {
        // Do stuff...
        return new RedirectResponse("~/");
    }
}

What is the correct way of telling Nancy to redirect to a specific URL inside a virtual directory?

I'm running Visual Studio on Windows 7 and IIS 7.5 (not IIS Express - as I have incoming traffic from third parties making callbacks)

This won't be a issue on production as the production deployment won't be in a virtual directory.


Solution

  • There are two ways to solve this.

    1) Use return Response.AsRedirect("~/");

    2) Use return new RedirectResponse(Context.ToFullPath("~/"));

    The first option is just a convenient wrapper around the second so either way yields the same result (https://github.com/NancyFx/Nancy/blob/85dfe8567d794ff3e766521a9fa0891832d4fc8a/src/Nancy/FormatterExtensions.cs#L51). The call to ToFullpath() is what corrects the /~/ you saw in your redirected URL.