如何配置 url 结合 url 分段和网络查询字符串 api rest url
How to configure the url with a combination of url segmentation and querystring for web api rest url
(使用网络 api 1.0)
我有一个场景,我的动作有多个参数,比如
GetCustomer(int id, string email)
其中 url 将指定为
得到
api/Customer/{id}/{email}
但我希望将 url 配置为
api/Customer/{id}?email={email}
所以它是url分段与查询字符串的组合。目前,当我尝试设置它时,出现以下错误。
路由 URL 不能以“/”或“~”字符开头,也不能包含“?”特点。
参数名称:routeUrl
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" }
).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
请帮助我解决这个问题提前谢谢
您的配置需要更新
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "CustomerApi_GetCustomer",
routeTemplate: "api/Customer/{id}",
defaults: new { controller = "Customer" action = "GetCustomer" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
如果您向 api/Customer/{id}?email={email}
提出请求
活页夹将自动匹配 GetCustomer(int id, string email)
与 url
中的 email
参数
(使用网络 api 1.0) 我有一个场景,我的动作有多个参数,比如 GetCustomer(int id, string email) 其中 url 将指定为
得到 api/Customer/{id}/{email}
但我希望将 url 配置为
api/Customer/{id}?email={email}
所以它是url分段与查询字符串的组合。目前,当我尝试设置它时,出现以下错误。
路由 URL 不能以“/”或“~”字符开头,也不能包含“?”特点。 参数名称:routeUrl
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" }
).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
请帮助我解决这个问题提前谢谢
您的配置需要更新
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "CustomerApi_GetCustomer",
routeTemplate: "api/Customer/{id}",
defaults: new { controller = "Customer" action = "GetCustomer" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
如果您向 api/Customer/{id}?email={email}
活页夹将自动匹配 GetCustomer(int id, string email)
与 url
email
参数