代码之家  ›  专栏  ›  技术社区  ›  CodingWithoutComments

使用ASP.NET MVC进行多参数路由

  •  125
  • CodingWithoutComments  · 技术社区  · 15 年前

    我们公司正在为我们的产品开发一个API,我们正在考虑使用ASP.NET MVC。在设计API时,我们决定使用下面这样的调用,让用户以XML格式从API请求信息:

    http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&api_key=b25b959554ed76058ac220b7b2e0a026

    如您所见,传递了多个参数(即 artist api_key )在ASP.NET MVC中, 艺术家 将是 controller , getImages 操作,但如何将多个参数传递给该操作?

    是否可以使用上述格式?

    3 回复  |  直到 9 年前
        1
  •  262
  •   Ryan Brunner    15 年前

    在MVC中,只需在操作方法中添加参数,就可以直接支持参数。给出如下操作:

    public ActionResult GetImages(string artistName, string apiKey)
    

    当给定如下URL时,MVC将自动填充参数:

    /Artist/GetImages/?artistName=cher&apiKey=XXX
    

    另一种特殊情况是名为“id”的参数。任何名为id的参数都可以放到路径中,而不是查询字符串中,因此类似于:

    public ActionResult GetImages(string id, string apiKey)
    

    将正确填充以下URL:

    /Artist/GetImages/cher?apiKey=XXX
    

    此外,如果您有更复杂的场景,您可以自定义MVC用于定位操作的路由规则。global.asax文件包含可以自定义的路由规则。默认情况下,规则如下:

    routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
    

    如果你想支持

    /Artist/GetImages/cher/api-key
    

    您可以添加类似以下的路由:

    routes.MapRoute(
                "ArtistImages",                                              // Route name
                "{controller}/{action}/{artistName}/{apikey}",                           // URL with parameters
                new { controller = "Home", action = "Index", artistName = "", apikey = "" }  // Parameter defaults
            );
    

    以及类似上面第一个例子的方法。

        2
  •  20
  •   Bernard Vander Beken Harald Coppoolse    9 年前

    从MVC5开始,还可以使用属性路由将URL参数配置移动到控制器。

    详细讨论如下: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

    总结:

    首先启用属性路由

     public class RouteConfig 
     {
         public static void RegisterRoutes(RouteCollection routes)
         {
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
             routes.MapMvcAttributeRoutes();
         } 
     }
    

    然后可以使用属性定义参数和可选的数据类型

    public class BooksController : Controller
    {
        // eg: /books
        // eg: /books/1430210079
        [Route("books/{isbn?}")]
        public ActionResult View(string isbn)
    
        3
  •  19
  •   George Stocker NotMe    15 年前

    您可以通过查询字符串传递任意参数,但也可以设置自定义路由,以便以静态方式处理它:

    http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&
                                      api_key=b25b959554ed76058ac220b7b2e0a026
    

    这可能是:

    routes.MapRoute(
        "ArtistsImages",
        "{ws}/artists/{artist}/{action}/{*apikey}",
        new { ws = "2.0", controller="artists" artist = "", action="", apikey="" }
        );
    

    因此,如果有人使用以下路径:

    ws.audioscrobbler.com/2.0/artists/cher/images/b25b959554ed76058ac220b7b2e0a026/
    

    它将把它们带到示例querystring所做的相同位置。

    上面只是一个例子,并没有应用业务规则和约束,你必须设置,以确保人们没有'黑客'的网址。