小编ain*_*ger的帖子

使用属性路由时替换Url.Link

我已将项目从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)

c# asp.net-web-api attributerouting asp.net-web-api-routing

17
推荐指数
1
解决办法
5395
查看次数

webapi2中DataMember和JsonProperty之间的区别

在webapi2中使用DataMember和JsonProperty有什么区别?任何性能差异?什么是首选使用?

谢谢!安德烈亚斯

c# serialization json asp.net-web-api

9
推荐指数
1
解决办法
4044
查看次数