【发布时间】:2016-09-16 23:09:39
【问题描述】:
(使用 Web api 1.0) 我有一个场景,我的动作有多个参数,比如 GetCustomer(int id,字符串电子邮件) 其中 url 将被指定为
获取 api/客户/{id}/{email}
但我希望将网址配置为
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 }
);
}
}
请提前帮我解决这个问题
【问题讨论】:
-
你有没有尝试使用 Route 属性?
-
展示你如何配置路由。
-
@FelipeOriani 我刚刚在问题中更新了我的路线配置,如果您需要更多信息,请告诉我,谢谢您的帮助
-
@Nkosi 我刚刚在问题中更新了我的路线配置,如果您需要更多信息,请告诉我,谢谢您的帮助
-
您说的是 web api,但您显示的路线是针对 MVC 而不是 web api。所以不清楚你指的是什么
标签: c# asp.net-web-api asp.net-web-api-routing