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:
我们公司正在为我们的产品开发一个API,我们正在考虑使用ASP。净MVC。在设计我们的API时,我们决定使用如下调用,让用户以XML格式从API中请求信息:
http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&api_key=b25b959554ed76058ac220b7b2e0a026
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?
如您所见,传递了多个参数(即artist和api_key)。在ASP。NET MVC, artist会是控制器,getImages动作,但是如何将多个参数传递给动作呢?
Is this even possible using the format above?
使用上面的格式,这是可能的吗?
3 个解决方案
#1
246
Parameters are directly supported in MVC by simply adding parameters onto your action methods. Given an action like the following:
在MVC中,只需在操作方法中添加参数,就可以直接支持参数。采取如下行动:
public ActionResult GetImages(string artistName, string apiKey)
MVC will auto-populate the parameters when given a URL like:
当给定一个URL时,MVC会自动填充参数,比如:
/Artist/GetImages/?artistName=cher&apiKey=XXX
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:
另一种特殊情况是名为“id”的参数。任何名为ID的参数都可以放在路径中,而不是querystring中,因此如下所示:
public ActionResult GetImages(string id, string apiKey)
would be populated correctly with a URL like the following:
将使用如下的URL正确填充:
/Artist/GetImages/cher?apiKey=XXX
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:
此外,如果您有更复杂的场景,您可以定制MVC用来定位操作的路由规则。你的全球。asax文件包含可以自定义的路由规则。默认情况下,规则如下:
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
如果你想支持一个url,比如
/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.
和上面第一个例子中的方法。
#2
18
You can pass arbitrary parameters through the query string, but you can also set up custom routes to handle it in a RESTful way:
您可以通过查询字符串传递任意参数,但您也可以设置自定义路由,以RESTful方式处理:
http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&
api_key=b25b959554ed76058ac220b7b2e0a026
That could be:
这可能是:
routes.MapRoute(
"ArtistsImages",
"{ws}/artists/{artist}/{action}/{*apikey}",
new { ws = "2.0", controller="artists" artist = "", action="", apikey="" }
);
So if someone used the following route:
所以如果有人使用以下路线:
ws.audioscrobbler.com/2.0/artists/cher/images/b25b959554ed76058ac220b7b2e0a026/
It would take them to the same place your example querystring did.
它将把它们带到您的示例querystring所处的位置。
The above is just an example, and doesn't apply the business rules and constraints you'd have to set up to make sure people didn't 'hack' the URL.
上面的例子只是一个例子,并没有应用必须设置的业务规则和约束,以确保人们没有“破解”URL。
#3
17
Starting with MVC 5, you can also use Attribute Routing to move the URL parameter configuration to your controllers.
从MVC 5开始,您还可以使用属性路由将URL参数配置移动到控制器。
A detailed discussion is available here: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
这里有详细的讨论:http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in- aspnet -mvc-5.aspx
Summary:
简介:
First you enable attribute routing
首先启用属性路由
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
Then you can use attributes to define parameters and optionally data types
然后,您可以使用属性来定义参数和可选数据类型。
public class BooksController : Controller
{
// eg: /books
// eg: /books/1430210079
[Route("books/{isbn?}")]
public ActionResult View(string isbn)
#1
246
Parameters are directly supported in MVC by simply adding parameters onto your action methods. Given an action like the following:
在MVC中,只需在操作方法中添加参数,就可以直接支持参数。采取如下行动:
public ActionResult GetImages(string artistName, string apiKey)
MVC will auto-populate the parameters when given a URL like:
当给定一个URL时,MVC会自动填充参数,比如:
/Artist/GetImages/?artistName=cher&apiKey=XXX
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:
另一种特殊情况是名为“id”的参数。任何名为ID的参数都可以放在路径中,而不是querystring中,因此如下所示:
public ActionResult GetImages(string id, string apiKey)
would be populated correctly with a URL like the following:
将使用如下的URL正确填充:
/Artist/GetImages/cher?apiKey=XXX
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:
此外,如果您有更复杂的场景,您可以定制MVC用来定位操作的路由规则。你的全球。asax文件包含可以自定义的路由规则。默认情况下,规则如下:
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
如果你想支持一个url,比如
/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.
和上面第一个例子中的方法。
#2
18
You can pass arbitrary parameters through the query string, but you can also set up custom routes to handle it in a RESTful way:
您可以通过查询字符串传递任意参数,但您也可以设置自定义路由,以RESTful方式处理:
http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=cher&
api_key=b25b959554ed76058ac220b7b2e0a026
That could be:
这可能是:
routes.MapRoute(
"ArtistsImages",
"{ws}/artists/{artist}/{action}/{*apikey}",
new { ws = "2.0", controller="artists" artist = "", action="", apikey="" }
);
So if someone used the following route:
所以如果有人使用以下路线:
ws.audioscrobbler.com/2.0/artists/cher/images/b25b959554ed76058ac220b7b2e0a026/
It would take them to the same place your example querystring did.
它将把它们带到您的示例querystring所处的位置。
The above is just an example, and doesn't apply the business rules and constraints you'd have to set up to make sure people didn't 'hack' the URL.
上面的例子只是一个例子,并没有应用必须设置的业务规则和约束,以确保人们没有“破解”URL。
#3
17
Starting with MVC 5, you can also use Attribute Routing to move the URL parameter configuration to your controllers.
从MVC 5开始,您还可以使用属性路由将URL参数配置移动到控制器。
A detailed discussion is available here: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
这里有详细的讨论:http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in- aspnet -mvc-5.aspx
Summary:
简介:
First you enable attribute routing
首先启用属性路由
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
Then you can use attributes to define parameters and optionally data types
然后,您可以使用属性来定义参数和可选数据类型。
public class BooksController : Controller
{
// eg: /books
// eg: /books/1430210079
[Route("books/{isbn?}")]
public ActionResult View(string isbn)