带有没有键的 QueryString 参数的 MVC Action

发布于 2025-01-02 14:59:08 字数 479 浏览 2 评论 0 原文

给定一个类似以下的 URL:

mysite.com/view/

?http://www.youtube.com/ 是否可以捕获操作参数中的查询字符串值。

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

我可以通过调用 QueryString[0] 来获取该值,这很好。

当查询字符串具有如下键时,它工作正常:

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

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

但我试图在没有键的情况下执行此操作。

编辑:更新了问题以反映我正在做的事情。我将未编码的 URL 作为查询字符串参数传递。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2025-01-09 14:59:08

可能有更简单的方法可以做到这一点,但这里有一种方法......

创建一个自定义路由处理程序。如果 URL-as-querystring 包含查询字符串本身,则获取 QueryString[0] 不太有效。因为最后一个“=”如果未编码,将被解释为查询值的开头。

http://mysite.com/view/?http://www.youtube.com/?whatever=something 将导致 something

因此,我们自己解析 URL:

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);
    }
*/
}

为您的路由注册处理程序:

routes.Add(new Route(
    "view",
    new RouteValueDictionary(new { controller = "Whatever", action = "View"}), 
    new NoKeyQueryStringHandler("url")
    )
);

操作:

// Might not want to hide Controller::View, but oh well...
public ActionResult View(string url)
{
   return Content(url);
}

URL 示例:

http://mysite.com/view/?http://www.youtube.com?whatever=something

... 会调用:

WhateverController::View("http://www.youtube.com?whatever=something");

闻起来“有点”像滥用URL 方案,但应该可以工作。

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.

圈圈圆圆圈圈 2025-01-09 14:59:08

为什么不使用路由参数而不是查询字符串来实现呢?

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

然后,您可以将该值作为操作方法的参数获取,而无需使用查询字符串:

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

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

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 = "")
{
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文