从ASP访问Web API路由。净MVC应用程序

时间:2022-11-22 04:13:09

I have a solution with 2 projects. One is an ASP.NET Web API project and the other is an ASP.NET MVC project. The web api will be consumed from various clients. If the 2 projects were one I could generate a link for my api like this:

我有两个项目的解决方案。一个是一个ASP。NET Web API项目和另一个是ASP。净MVC项目。web api将从各种客户机中使用。如果这两个项目是一个,我可以为我的api生成这样的链接:

@Url.RouteUrl("ActionApi", new { httpRoute = string.Empty, controller = "User", action = "AddChildAsync" });  

But now that these 2 projects are separated I cannot do this because the mvc appliction cannot find the configuration for the web api (although I have a project reference from the mvc app to the web api). So is there any elegant way to access web api configuration and generate links dynamically? Thanks in advance.

但是现在这两个项目被分开了,我不能这样做,因为mvc应用程序无法找到web api的配置(尽管我有一个从mvc应用到web api的项目引用)。那么有什么优雅的方式来访问web api配置并动态生成链接吗?提前谢谢。

2 个解决方案

#1


2  

You should be able to access it by changing httproute = true.

您应该能够通过更改httproute = true来访问它。

@Url.RouteUrl("ActionApi", new { httpRoute = true, controller = "User", action = "AddChildAsync" });

Or you could do this to specify it a webapi route.

或者你可以这样指定它为webapi路径。

@Url.HttpRouteUrl("ActionApi", new { controller = "User", action = "AddChildAsync" });  

But you do need the WebApi registration done in your Global.asax and have the NuGet WebApi package installed.

但是您需要在您的全局中完成WebApi注册。asax和安装NuGet WebApi包。

Also, typically actions aren't included in the route as you've referenced above in your links. They're typically unnamed and provided by request (GET, POST PUT, ect). Those are omitted unless your route config looks something like this.

而且,通常的操作不包括在您在链接中引用的路径中。它们通常是匿名的,由请求(GET、POST PUT等)提供。除非您的路由配置看起来像这样,否则将忽略这些。

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional } );

#2


1  

I had a similar issue once.

我曾经遇到过类似的问题。

My solution was creating interfaces for each one of the WebApi controllers that could be shared among your projects (MVC and WebApi). To each one of the WebApi controllers add an action (I will refer to it as 'Routes' action) that returns serialized Collection of key-value elements, where keys are interface (WebApi) method names and values are objects that contain routes and http methods (Get,Post etc) (It is constructed in WebApi project so you have access to it's routing table).On MVC application start you can issue a request to your WebApi Routes action and get all it's routes. Then you can store it in a global readonly dictionary for a quick access. Getting a particular route becomes a trivial task because your interface method names are keys to the dictionary you filled earlier.

我的解决方案是为每个WebApi控制器创建接口,这些控制器可以在项目之间共享(MVC和WebApi)。每一个WebApi控制器添加一个动作(我将把它称为“路线”行动)序列化返回键值元素的集合,钥匙在哪里接口(WebApi)方法名称和值对象包含路线和http方法(Get、Post等)(它是建于WebApi项目你可以访问它的路由表)。在MVC应用程序启动中,您可以向您的WebApi路由操作发出请求并获取所有的路由。然后可以将它存储在一个全局只读字典中,以便快速访问。获取特定的路由成为一项微不足道的任务,因为您的接口方法名是前面填充的字典的键。

#1


2  

You should be able to access it by changing httproute = true.

您应该能够通过更改httproute = true来访问它。

@Url.RouteUrl("ActionApi", new { httpRoute = true, controller = "User", action = "AddChildAsync" });

Or you could do this to specify it a webapi route.

或者你可以这样指定它为webapi路径。

@Url.HttpRouteUrl("ActionApi", new { controller = "User", action = "AddChildAsync" });  

But you do need the WebApi registration done in your Global.asax and have the NuGet WebApi package installed.

但是您需要在您的全局中完成WebApi注册。asax和安装NuGet WebApi包。

Also, typically actions aren't included in the route as you've referenced above in your links. They're typically unnamed and provided by request (GET, POST PUT, ect). Those are omitted unless your route config looks something like this.

而且,通常的操作不包括在您在链接中引用的路径中。它们通常是匿名的,由请求(GET、POST PUT等)提供。除非您的路由配置看起来像这样,否则将忽略这些。

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional } );

#2


1  

I had a similar issue once.

我曾经遇到过类似的问题。

My solution was creating interfaces for each one of the WebApi controllers that could be shared among your projects (MVC and WebApi). To each one of the WebApi controllers add an action (I will refer to it as 'Routes' action) that returns serialized Collection of key-value elements, where keys are interface (WebApi) method names and values are objects that contain routes and http methods (Get,Post etc) (It is constructed in WebApi project so you have access to it's routing table).On MVC application start you can issue a request to your WebApi Routes action and get all it's routes. Then you can store it in a global readonly dictionary for a quick access. Getting a particular route becomes a trivial task because your interface method names are keys to the dictionary you filled earlier.

我的解决方案是为每个WebApi控制器创建接口,这些控制器可以在项目之间共享(MVC和WebApi)。每一个WebApi控制器添加一个动作(我将把它称为“路线”行动)序列化返回键值元素的集合,钥匙在哪里接口(WebApi)方法名称和值对象包含路线和http方法(Get、Post等)(它是建于WebApi项目你可以访问它的路由表)。在MVC应用程序启动中,您可以向您的WebApi路由操作发出请求并获取所有的路由。然后可以将它存储在一个全局只读字典中,以便快速访问。获取特定的路由成为一项微不足道的任务,因为您的接口方法名是前面填充的字典的键。