简体   繁体   English

mvc中强类型的web api路由端点

[英]Strongly typed web api route endpoints in mvc

So I have two separate projects (one Web Api 2 and one MVC) like this diagram: 所以我有两个单独的项目(一个Web Api 2和一个MVC),如下图所示: 在此输入图像描述

The MVC has controllers and a service layer. MVC具有控制器和服务层。 The services from the MVC app call to the web api controllers. MVC应用程序的服务调用web api控制器。 For example: 例如:

await _service.GetGiftCards("/api/GiftCards/ViewAllByUser", email);

The Web Api controllers have their routes defined like so: Web Api控制器的路由定义如下:

[RoutePrefix("api/giftcards")]
[Route("ViewAllByUser")]
    public async Task<List<GiftCard>> GetGiftCardsForUser(string email){}

So to define the endpoint route in the MVC app I simply pass a string like "/api/GiftCards/ViewAllByUser" above. 因此,要在MVC应用程序中定义端点路由,我只需传递上面的“/ api / GiftCards / ViewAllByUser”字符串。

My question is, is there a better way to sort of "strongly type" the endpoints of the Web Api routes that are defined so I can do something like?: 我的问题是,有没有更好的方法来“强类型”定义的Web Api路由的端点,所以我可以做类似的事情:

await _service.GetGiftCards(GiftCardController.ViewAllByUser, email);

I guess at a minimum I could always just store the endpoint strings in a static class like so, so they at least can all be updated in one place: 我想至少我总是可以将端点字符串存储在静态类中,所以它们至少可以在一个地方更新:

public static class ApiEndpoints(){
     public string GetAllGiftCards = "api/GiftCards/ViewAllByUser";
}

but I'm looking to know if there are better ways or other suggestions. 但我想知道是否有更好的方法或其他建议。 Thanks! 谢谢!

API routes shouldn't specify actions. API路由不应指定操作。 You want your routes to be logical paths to a record or group of records. 您希望路由成为记录或记录组的逻辑路径。 Example, in your case the route should look something like this: 例如,在您的情况下,路由应如下所示:

GET
api/giftcards/{userID:int:min(1)}

You want to be able to walk up the url basically and get what you would expect. 你希望能够基本上走上网址并获得你期望的结果。 In the case of the example route you would get gift cards based on the user id. 在示例路线的情况下,您将获得基于用户ID的礼品卡。 If you were to take off the user id page and just call api/giftcards you would expect to get all gift cards by all users. 如果您要取消用户ID页面并只需拨打api / giftcards,您就可以获得所有用户的所有礼品卡。 I'm using an ID here but you would do the same with email. 我在这里使用了一个ID,但你会用电子邮件做同样的事情。

Pleas try with 'ActionName' Attribute on action like this : 请尝试使用'ActionName'属性进行操作,如下所示:

 [ActionName("SelectAll")]
        public IEnumerable<Customer> Get()
        {
       ...
        }

calling this action name like: 将此操作名称称为:

$("#getdata").click(function () {
  var options = {};
  options.url = "/api/customer/SelectAll";
  options.type = "GET";
  ...
  ...
  $.ajax(options);
});

note: 'getdata' is id of control which click event will be fired and calling 'api method' and getdata from api 注意:'getdata'是控件的id,将触发哪个click事件并从api调用'api method'和getdata

Here's a library that may be what you're looking for. 这是一个可能正是您正在寻找的图书馆

Although I like static strings so you don't always have to show future developers on your team how to use said library when updates are needed on the clients. 虽然我喜欢静态字符串,但您不必总是向团队中的未来开发人员展示如何在客户端需要更新时使用该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM