ain*_*ger 17 c# asp.net-web-api attributerouting asp.net-web-api-routing
我已将项目从webapi升级到webapi2,现在正在使用属性路由.我有一个方法,我使用Url helper获取url.哪个是替换Url helper的最佳方法(因为这不适用于属性).
我的旧用法示例代码:
protected Uri GetLocationUri(object route, string routeName = WebApiConfig.RouteDefaultApi)
{
string uri = Url.Link(routeName, route);
return new Uri(uri);
}
Run Code Online (Sandbox Code Playgroud)
路线配置:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: RouteDefaultApi,
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = "Default" }
);
}
Run Code Online (Sandbox Code Playgroud)
用法:
Uri myUrl = GetLocationUri(route: new { action = "images", id = eventId });
Run Code Online (Sandbox Code Playgroud)
Kir*_*lla 41
RouteDefaultApi当您想要生成到控制器/操作的属性路由的链接时,为什么要尝试使用传统路径?
以下是如何使用Url.Link和属性路由的示例用法:
[Route("api/values/{id}", Name = "GetValueById")]
public string GetSingle(int id)
Url.Link("GetValueById", new { id = 10 } );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5395 次 |
| 最近记录: |