Redirect to metadata BaseUrl not dynamic?

Hi there,

public override string GetBaseUrl(IRequest httpReq)

or

public override string ResolveAbsoluteUrl(string virtualPath, IRequest httpReq)

wont let me override ‘WebHostUrl’ im case of the initial request.

Given:

  • I’m accesing the api via a port forward.
  • I have no default.cshtml

When visiting http://external _url:10001, wich forwards tot the device, it will redirect me to http://localhost:8000/metadata, wich is the original URL the Api is started on. No difference if i setup DefaultRedirectLocation to something in the API, it will just put http://localhost:8000 in front of that then.

The first request i do, returns a 302 StatusCode, with a Location Header to the ‘wrong’ URI.

I just cant find where or how i can override this. I cant use WebHostUrl in HostConfig, because it’s static, and i need it to be request based dynamic.

Can you point me in a right direction?

You could register a RawHttpHandler to intercept the request and provide your own redirect, e.g:

RawHttpHandlers.Add(req => req.PathInfo == "/"
    ? new RedirectHttpHandler { AbsoluteUrl = redirectUrl } 
    : null);

BTW I’ve just checked in a change that will use ResolveAbsoluteUrl() if your redirect path starts with ~/.

This change is available from v4.5.11 that’s now available on MyGet. Please let me know if it resolves your issue.

Not quite!

It seems the update to 4.5.11 doesn’t account this redirect kind of request to /metadata or something else; it still does the same thing, even if i put ~/metadata (e.g.) in the DefaultRedirectPath.

However, with the RawHttpHandler there is something i got working now.

For me it works doing these 2 things:

   SetConfig(new HostConfig
			{
...
        DefaultRedirectPath = "/workflow/tile"
    });


RawHttpHandlers.Add(req => req.PathInfo == "/" ? new ServiceStack.Host.Handlers.RedirectHttpHandler { AbsoluteUrl = Config.DefaultRedirectPath } : null);

AND

public override string GetBaseUrl(ServiceStack.Web.IRequest httpReq)
		{
			var userHostName = ((System.Net.HttpListenerRequest)httpReq.OriginalRequest).UserHostName;
			if (httpReq.IsSecureConnection)
				return "https://{0}".Fmt(userHostName);
			else
				return "http://{0}".Fmt(userHostName);
		}

Still in doubt how i can do this nicer…

Apologies for that, the BaseUrl was being cached which prevented it from calling ResolveAbsoluteUrl() which should be resolved from this commit. Can you try using a ~/ url again with the latest v4.5.11 version on MyGet, You’ll need to clear your NuGet cache in order to re-download the latest v4.5.11 packages.

Will try shortly, thanks again!