1

Given a URL like:

mysite.com/view/?http://www.youtube.com/

Is it possible to capture the Query String value in the parameters of an action.

public ActionResult View(string query = "")
{
...
}

I can get the value by calling QueryString[0] which is fine.

It works fine when the query string has a key like:

mysite.com/view/?query=http://www.youtube.com/

public ActionResult View(string query = "")
{
...
}

But I'm trying to do it without a key.

Edit: Updated the question to reflect what I'm doing. I'm passed a URL as un-encoded url as a query string parameter.

Phill
  • 18,398
  • 7
  • 62
  • 102
  • With an url like yours mysite.com/view/?123 - Wouldn't that give you a querystring variable with the name (key) 123 and an empty value? So it still isn't without a key, it's without a value. – Christofer Eliasson Feb 04 '12 at 13:31
  • @ChristoferEliasson I updated my question to reflect what I'm doing. No it gives a null key with a value. – Phill Feb 04 '12 at 13:32

2 Answers2

0

Why not do it with a route parameter instead of a querystring?

MapRoute(null, "view/{*urlParam}", ...

You can then get the value as an argument to your action method without using the querystring:

mysite.com/view/http://www.youtube.com/

public ActionResult View(string urlParam = "")
{
...
}
danludwig
  • 46,965
  • 25
  • 159
  • 237
  • The URL parameter would need to be encoded, and I'm passed the URL as a query string. Trying to solve the problem without having to get things changed by others. – Phill Feb 04 '12 at 13:58
  • Note that it's actually possible to pass the URL unencoded. See [Hanselman's blog post](http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx) about the approach. But if you want to keep it as a query, see my answer. – JimmiTh Feb 04 '12 at 15:01
0

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.

JimmiTh
  • 7,389
  • 3
  • 34
  • 50