There are probably much easier ways to do this, but here's one approach...
Make a custom route handler. Getting QueryString[0]
won't quite work, if the URL-as-querystring includes a querystring itself. Because the last "=", if unencoded, will be interpreted as the beginning of the query value.
I.e. http://mysite.com/view/?http://www.youtube.com/?whatever=something
will result in something
.
So, instead, we parse the URL ourselves:
public class NoKeyQueryStringHandler : MvcRouteHandler
{
private string routeDataKey;
// routeDataKey: the key to use for storing our query in routeData.Values
public NoKeyQueryStringHandler(string routeDataKey)
{
this.routeDataKey = routeDataKey;
}
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string[] parts = requestContext.HttpContext.Request.Url
.OriginalString.Split(new [] {'?'}, 2);
string value = parts.Length > 1 ? parts[1] : "";
requestContext.RouteData.Values[routeDataKey] = value;
return base.GetHttpHandler(requestContext);
}
// Handler that doesn't take care of querystring-in-querystring:
/*
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
NameValueCollection query =
requestContext.HttpContext.Request.QueryString;
string value = query.Count > 0 ? query[0] : "";
requestContext.RouteData.Values[routeDataKey] = value;
return base.GetHttpHandler(requestContext);
}
*/
}
Register the handler for your route:
routes.Add(new Route(
"view",
new RouteValueDictionary(new { controller = "Whatever", action = "View"}),
new NoKeyQueryStringHandler("url")
)
);
Action:
// Might not want to hide Controller::View, but oh well...
public ActionResult View(string url)
{
return Content(url);
}
URL example:
http://mysite.com/view/?http://www.youtube.com?whatever=something
... will call:
WhateverController::View("http://www.youtube.com?whatever=something");
Smells "a bit" like abuse of the URL scheme, but should work.