使用ASP.NET MVC多参数路由 [英] Routing with Multiple Parameters using ASP.NET MVC

查看:333
本文介绍了使用ASP.NET MVC多参数路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的公司正在开发我们的产品的API,我们正在考虑使用ASP.NET MVC。在设计我们的API,我们决定采用类似下面呼吁用户从API请求信息以XML格式:

Our company is developing an API for our products and we are thinking about using ASP.NET MVC. While designing our API, we decided to use calls like the one below for the user to request information from the API in XML format:

<一个href="http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&api_key=b25b959554ed76058ac220b7b2e0a026">http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&api_key=b25b959554ed76058ac220b7b2e0a026

正如你所看到的,多参数传递(即艺术家 API_KEY )。在ASP.NET MVC中,艺术家将成为控制器 getImages 的行动,但我将如何传递多个参数的动作?

As you can see, multiple parameters are passed (i.e. artist and api_key). In ASP.NET MVC, artist would be the controller, getImages the action, but how would I pass multiple parameters to the action?

使用上述格式这甚至可能吗?

Is this even possible using the format above?

推荐答案

参数是通过简单地添加参数直接支持MVC到您的操作方法。鉴于类似下面的操作:

Parameters are directly supported in MVC by simply adding parameters onto your action methods. Given an action like the following:

public ActionResult GetImages(string artistName, string apiKey)

在给定像一个网址

MVC将自动填充的参数:

MVC will auto-populate the parameters when given a URL like:

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

一个额外的特殊情况是一个名为身份证参数。任何命名参数ID可投入的路径,而不是查询字符串,所以是这样的:

One additional special case is parameters named "id". Any parameter named ID can be put into the path rather than the querystring, so something like:

public ActionResult GetImages(string id, string apiKey)

将正确填充类似下面的网址:

would be populated correctly with a URL like the following:

/Artist/GetImages/cher?apiKey=XXX

另外,如果你有更复杂的情况下,您可以自定义MVC使用定位的操作的路由规则。您的Global.asax文件包含了可自定义的路由规则。默认情况下,规则是这样的:

In addition, if you have more complicated scenarios, you can customize the routing rules that MVC uses to locate an action. Your global.asax file contains routing rules that can be customized. By default the rule looks like this:

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

如果你想支持像一个网址

If you wanted to support a url like

/Artist/GetImages/cher/api-key

您可以添加一个路径,如:

you could add a route like:

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

和类似于第一例子的上述方法

and a method like the first example above.

这篇关于使用ASP.NET MVC多参数路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆