标签: asp.net-web-api-routing

组合密钥资源REST服务

我在工作中遇到了一个问题,我无法找到有关在RESTful Web服务中针对主键是其他资源ID的组合的资源执行CRUD操作的通常标准或实践的信息.我们使用MVC WebApi来创建控制器.例如,我们有三个表:

  • Product:PK = ProductId
  • Part:PK = PartId
  • ProductPartAssoc:PK =(ProductId,PartId)

产品可以包含许多部件,而部件可以是许多产品的组件.关联表还包含与关联本身相关的其他信息,而不是可编辑的信息.

我们有ProductsControllerPartsController类使用定义为的路由模板处理通常的GET/PUT/POST/DELETE操作:{controller}/{id}/{action}以便以下IRI工作:

  • GET,POST /api/Products- 返回所有产品,创建新产品
  • GET,PUT,DELETE /api/Products/1- 检索/更新/删除产品1
  • GET,POST /api/Parts- 返回所有部件,创建一个新部件
  • GET,PUT,DELETE /api/Parts/2- 检索/更新/删除第2部分
  • 获取 /api/Products/1/Parts- 获取产品1的所有部件
  • 获取 /api/Parts/2/Products- 获取第2部分为组件的所有产品

我遇到问题的方法是如何为ProductPartAssoc资源定义路径模板.获取关联数据的路由模板和IRI应该是什么样的?坚持惯例,我希望如下:

  • GET,POST /api/ProductPartAssoc- 返回所有关联,创建关联
  • GET,PUT,DELETE /api/ProductPartAssoc/[1,2]- 检索/更新/删除产品1和第2部分之间的关​​联

我的同事发现这在美学上令人不悦,并且似乎认为最好不要有一个ProductPartAssocController类,而是添加其他方法ProductsController来管理关联数据:

  • GET,PUT,DELETE /api/Products/1/Parts/2- 获取产品1和第2部分之间关联的数据,而不是第2部分作为第1部分成员的数据,这通常是基于其他示例的情况,例如/Book/5/Chapter/3我在其他地方看到的情况.
  • POST没有任何线索,他们期望IRI看起来像什么.不幸的是,他们是决策者.

在一天结束时,我想我要求的是验证,或者我可以指出的方向,并说"看,这是其他人做的."

处理复合键识别的资源的典型做法是什么?

rest composite-key asp.net-web-api attributerouting asp.net-web-api-routing

23
推荐指数
1
解决办法
8764
查看次数

RoutePrefix与路由

据我所知,这RoutePrefix不会自己添加到路由表的路由.在您的操作上,您需要Route声明一个属性.我很难找到一个权威的博客/ msdn页面/一些东西,说明为什么defalut RoutePrefix不会添加到路由表的路由.

有没有人有一个确实含有此权威的权威帖子,如果是这样,你会告诉我它是谁.非常感谢你.

编辑 以澄清我的问题

什么都不行

[RoutePrefix("api/Steve")]
public class SteveController : ApiController
{
    public int get(){return 1000000;}
}
Run Code Online (Sandbox Code Playgroud)

作品

[RoutePrefix("api/Steve")]
public class SteveController : ApiController
{
    [Route("")]
    public int get(){return 1000000;}
}
Run Code Online (Sandbox Code Playgroud)

上面的场景是有效的,因为我们明确表示对该路径的get操作SteveController有一条空路线.一旦我们这样做,路线就会被添加到RouteTable

第一种情况不起作用,因为只是使用RoutePrefix不会向路由表添加任何内容.RoutePrefix本身不会产生路线.这似乎是常识,我想知道一个说明原因的来源.优先受尊敬的社区成员,即Jon Skeet或Microsoft团队成员.

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

23
推荐指数
2
解决办法
4万
查看次数

如何在不使用AttributeRouting在Route属性上指定名称的情况下生成WebApi2 URL?

我已将ASP.NET MVC5应用程序配置为使用AttributeRouting进行WebApi:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    }
}
Run Code Online (Sandbox Code Playgroud)

我有ApiController如下:

[RoutePrefix("api/v1/subjects")]
public class SubjectsController : ApiController
{
    [Route("search")]
    [HttpPost]
    public SearchResultsViewModel Search(SearchCriteriaViewModel criteria)
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

我想为我的WebApi控制器操作生成一个URL,而不必指定显式路由名称.

根据CodePlex上的这个页面,所有MVC路由都有一个不同的名称,即使它没有指定.

如果没有指定的路由名称,Web API将生成默认路由名称.如果特定控制器上的操作名称只有一个属性路由,则路径名称将采用"ControllerName.ActionName"形式.如果该控制器上有多个具有相同操作名称的属性,则会添加后缀以区分路径:"Customer.Get1","Customer.Get2".

在ASP.NET上,它没有确切地说明默认命名约定是什么,但它确实表明每个路由都有一个名称.

在Web API中,每个路由都有一个名称.路由名称对于生成链接非常有用,因此您可以在HTTP响应中包含链接.

基于这些资源,以及StackOverflow用户Karhgath回答,我被认为以下会产生一个指向我的WebApi路由的URL:

@(Url.RouteUrl("Subjects.Search"))
Run Code Online (Sandbox Code Playgroud)

但是,这会产生错误:

在路径集合中找不到名为"Subjects.Search"的路径.

我已经根据我在StackOverflow上找到的其他答案尝试了一些其他变体,没有成功.

@(Url.Action("Search", "Subjects", new { httproute = "" }))

@(Url.HttpRouteUrl("Search.Subjects", new {}))
Run Code Online (Sandbox Code Playgroud)

实际上,即使在属性中提供Route名称,也只能使用:

@(Url.HttpRouteUrl("Search.Subjects", new {}))
Run Code Online (Sandbox Code Playgroud)

其中"Search.Subjects"被指定为Route属性中的路径名称.

我不想被迫为我的路线指定一个唯一的名称.

如何生成WebApi控制器操作的URL而无需在Route属性中明确指定路由名称?

CodePlex的默认路由命名方案是否可能已更改或记录错误?

有没有人对正确检索已使用AttributeRouting设置的路由的URL的方法有所了解?

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

21
推荐指数
2
解决办法
2077
查看次数

Web API路由支持GUID和整数ID

如何支持GETGUID和整数的路由?我意识到GUID不是理想的,但它现在就是它.我想添加对整数的支持,以便用户更容易记住和传达应该是唯一的"密钥".

示例路线:

testcases/9D9A691A-AE95-45A4-A423-08DD1A69D0D1   
testcases/1234
Run Code Online (Sandbox Code Playgroud)

我的WebApiConfig:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    var routes = config.Routes;

    routes.MapHttpRoute("DefaultApiWithAction", 
        "Api/{controller}/{action}");

    routes.MapHttpRoute("DefaultApiWithKey",
        "Api/{controller}/{key}",
        new { action = "Get" },
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get), key = @"^\d+$" });

    routes.MapHttpRoute("DefaultApiWithId", 
        "Api/{controller}/{id}", 
        new { action = "Get" }, 
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

    routes.MapHttpRoute("DefaultApiGet", 
        "Api/{controller}", 
        new { action = "Get" }, 
        new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

    routes.MapHttpRoute("DefaultApiPost", 
        "Api/{controller}", 
        new { action = "Post" }, …
Run Code Online (Sandbox Code Playgroud)

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

20
推荐指数
1
解决办法
1万
查看次数

多个控制器类型具有相同的路由前缀ASP.NET Web Api

是否可以将GET和POST分离为单独的API控制器类型并使用相同的路径前缀访问它们?

这是我的控制器:

[RoutePrefix("api/Books")]
public class BooksWriteController : EventStoreApiController
{
    [Route("")]
    public void Post([FromBody] CommandWrapper commandWrapper){...}
}

[RoutePrefix("api/Books")]
public class BooksReadController : MongoDbApiController
{
    [Route("")]
    public Book[] Get() {...}


    [Route("{id:int}")]
    public Book Get(int id) {...}
}
Run Code Online (Sandbox Code Playgroud)

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

19
推荐指数
1
解决办法
2万
查看次数

Web API路由到操作名称

我需要一个控制器来返回JSON以供JavaScript使用,所以我继承了ApiController类,但它的行为并不像我预期的那样.Apress书籍Pro ASP.NET MVC 4以及我发现的大多数在线示例给出了以下示例:

public class ServicesController : ApiController
{
    public string[] MethodFruit()
    {
        return new string[] { "Apple", "Orange", "Banana" };
}
Run Code Online (Sandbox Code Playgroud)

通过URL访问:

http://mysite/services/methodfruit
Run Code Online (Sandbox Code Playgroud)

但这永远不会奏效 - 找不到资源.我可以使用的唯一方法是让控制器为每个HTTP动词包含不同的方法,然后:

http://mysite/api/services
Run Code Online (Sandbox Code Playgroud)

哪个调用GET方法.

我检查了Apress网站,但他们似乎没有任何论坛,目前的源代码是在VS 2012,我没有使用.我检查了源文件,他们似乎认为前一种方法应该有效.前一种方法是否不再受支持?

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

18
推荐指数
3
解决办法
6万
查看次数

ASP.NET Web API多个RoutePrefix

开源属性路由允许具有多个路由前缀.为什么ASP.NET Web API 2.0不允许具有多个RoutePrefix().

[RoutePrefix("api/v1/{abc}/Entity")]
[RoutePrefix("api/v1/{abc}/{xyz?}/Entity")]
public class MyApiController : ApiController
{
   [Route("")]
   public IHttpResult Get()
   {
      return Ok("Hello World");
   }
}
Run Code Online (Sandbox Code Playgroud)

attributerouting asp.net-web-api-routing asp.net-web-api2

18
推荐指数
1
解决办法
9332
查看次数

+(加号)登录Web API路由

我正在使用asp.net web api项目,我必须通过帖子传递手机号码.但我不能返回一个加号.

我的路线:

config.Routes.MapHttpRoute( 
    name: "SmsRoute",
    routeTemplate: "rest/sms/{country}/{company}/phone/{mobilenumber}",
    defaults: new { controller = "Sms", action = "PostSms" });
Run Code Online (Sandbox Code Playgroud)

控制器:

public HttpResponseMessage PostSms(string country, string company, string mobilenumber)
{
    return Request.CreateResponse( HttpStatusCode.Created );
}
Run Code Online (Sandbox Code Playgroud)

当我在fiddler上运行这个post方法时:

http://index.html/rest/sms/se/company/phone/46700101010/messages/out
Run Code Online (Sandbox Code Playgroud)

我收到了这个回复:

在此输入图像描述

但我希望它能够向我展示加号.

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

18
推荐指数
2
解决办法
5258
查看次数

使用属性路由时替换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
查看次数

如何正确映射WebAPI路由

我正在使用Web API为类似Twitter的网站构建API,并且无法映射路由

我对User控制器执行以下操作:

public User Get(string firstname, string lastname)
public User Get(Guid id)
public User Friends(Guid id)
public User Followers(Guid id)
public User Favorites(Guid id)
Run Code Online (Sandbox Code Playgroud)

所需的路线和生成的文档应该是:

api/users?firstname={firstname}&lastname={lastname}
api/users/{id}
api/users/{id}/friends
api/users/{id}/followers
api/users/{id}/favorites
Run Code Online (Sandbox Code Playgroud)

在WebApiConfig.cs中我有:

config.Routes.MapHttpRoute(
    "2",
    "api/{controller}/{id}",
    new { action = "get", id = RouteParameter.Optional }
);


config.Routes.MapHttpRoute(
     "1",
     "api/{controller}/{id}/{action}"
);
Run Code Online (Sandbox Code Playgroud)

如何正确映射WebAPI路由?

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

17
推荐指数
3
解决办法
5万
查看次数