简体   繁体   English

如何在ASP.Net MVC 3中路由遗留的QueryString参数?

[英]How to route legacy QueryString parameters in ASP.Net MVC 3?

I am using a third party service that does an async callback to a URL I provide to them. 我正在使用第三方服务,该服务对我提供给他们的URL执行异步回调。 So I tell them to use http://www.mysite.com/Status/Incoming . 所以我告诉他们使用http://www.mysite.com/Status/Incoming This must obviously map to an Incoming() method on my StatusController. 这显然必须映射到我的StatusController上的Incoming()方法。

However, what I don't have control over is the format of the parameters they call my URL with. 但是,我无法控制的是他们称之为URL的参数格式。 Eg They will do a callback such as: http://www.mysite.com/Status/Incoming?param1=val1&param2=val2&param3=val3 例如,他们将进行回调,例如: http ://www.mysite.com/Status/Incoming?param1 = val1&param2 = val2&param3 = val3

I want to map this to the parameters of my action method: Incoming(string param1, string param2, int param3) 我想将此映射到我的action方法的参数:Incoming(string param1,string param2,int param3)

How do I do this? 我该怎么做呢?

I have found a lot of stuff about custom routing, but nothing about legacy QueryString parameters. 我发现了许多关于自定义路由的东西,但没有关于遗留的QueryString参数。

There is no such thing as legacy query string parameters . 没有遗留查询字符串参数这样的东西。 There are query string parameters and they are part of the HTTP specification. 有查询字符串参数,它们是HTTP规范的一部分。 And assuming that the http://www.mysite.com/Status/Incoming?param1=val1&param2=val2&param3=val3 url is called you don't need any route to make it map to the following action (the default route will do just fine): 假设调用了http://www.mysite.com/Status/Incoming?param1=val1&param2=val2&param3=val3 url,则不需要任何路由使其映射到以下操作(默认路由只会执行精细):

public ActionResult Incoming(string param1, string param2, string param3)
{
    ...
}

The default model will take care of binding those values. 默认模型将负责绑定这些值。

Why not use a catch all? 为什么不使用全部捕获?

        routes.MapRoute(
            "Incoming", 
            "Status/Incoming/{*path}", // URL with parameters
            new { controller = "Status", action = "Incoming"} 
        );

then in your controller, 然后在你的控制器中

  public ActionResult Incoming(string path){
        //  the next line would probably be better off in a model binder, but this works:
        var dictionary = path
            .Substring(path.IndexOf("?")+1)
            .Split("&")
            .Select(x =>
                        {
                            var kvArray = x.Split("=");
                            return new KeyValuePair<string, string>(kvArray[0], kvArray[1]);
                        })
                        .ToDictionary(x=>x.Key,x=>x.Value);
           return Incoming(dictionary);
  }

  public ActionResult Incoming(Dictionary<string,string> dictionary){
       //do stuff
  }

All that being said, I think using the Request.QueryString is probably a better approach. 所有这一切,我认为使用Request.QueryString可能是一个更好的方法。 As long as you are using MVC, it is accessible from your controller. 只要您使用MVC,就可以从控制器访问它。 However, if you can guarantee that the correct parameters will be passed then Darin's approach is going to be the best choice. 但是,如果您能保证通过正确的参数,那么Darin的方法将是最佳选择。

When I've had to deal with this before now, I just use the "legacy" call of Request.QueryString. 之前我必须处理这个问题,我只是使用Request.QueryString的“遗留”调用。 It still works, even if it isn't very graceful. 它仍然有效,即使它不是很优雅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM