MVC4 -> Mapping one path to another ( not a controller )

190 views Asked by At

OK, let me ask this a different way. We have a web app. We have paid and trial subscribers. Each of them current has a folder named after their user id, and that's where their code lives. The code gets their user id from the URL, so we can't change the URL. I want to consolidate the location of our code, so that people go to the same URL as they are today ( sitename/username ), but it will return the application code ( which is a folder structure ) from a single location. I've tried everything I can think of, but Server.Transfer can't transfer to a folder, transfering to the index.html does not work, Server.TransferLocation and ( of course ) Response.Redirect change the URL on the client side.

I have tried doing this in a RouteHandler, and in a Controller, it makes no difference, I cannot find a way to redirect the user transparently to a central code base. Ideally, I'd do it in code, so I can validate what sort of user they are and redirect them accordingly.

After many iterations, here is the core code, right now:

public class FarmHttpHandler : IRouteHandler
{
    enum UserType { Invalid = -1, Standard = 1, Tester = 2, Developer = 3}

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // Note - this has to be a valid URL, or it will loop forever, as our HTTP handler is at the base level.
        // We need to add a 'bad request' page, or it could default to the demo ( this would be a support nightmare though, if someone mistyped their URL,
        // they would think it was fine, at first.
        string redirect = "/Home/Error";

        var routeValues = requestContext.RouteData.Values;

        if (routeValues.ContainsKey("farmName"))
        {
            string farmName = routeValues["farmName"].ToString();


            string baseUrl = "/";// HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "/");

            switch(GetUserType(farmName))
            {
                case UserType.Invalid:
                    break;
                case UserType.Developer:
                    redirect = baseUrl + Settings.BetaAppPath;
                    break;
                case UserType.Standard:
                    redirect = baseUrl + Settings.FullAppPath;
                    break;
                case UserType.Tester:
                    redirect = baseUrl + Settings.TesterAppPath;
                    break;
            }
        }

        HttpContext.Current.Server.TransferRequest(redirect);

        requestContext.HttpContext.RewritePath(redirect);


        return requestContext.HttpContext.Handler;
    }

This gets called, but the rewritepath doesn't do anything ( the error says no IHttpHandler was returned ) and the TransferRequest works, but the URL changes in the browser. The code also uses relative paths for images, etc, and these do not load ( not sure if I can actually fix this ).

1

There are 1 answers

0
cgraus On

The answer is

         return BuildManager.CreateInstanceFromVirtualPath(URl, typeof(Page)) as Page; 

That's how you return a redirect from a routehandler. Sadly, it won't work for me, the browser can't work out to redirect the requests within our web app, and it doesn't make requests to a full URL, which means I can't detect them to remap them.