从ApiController和动作名字获取网址,包含控制器和ApiControllers项目 [英] Get Url from ApiController and Action names, in a project containing Controllers and ApiControllers

查看:1323
本文介绍了从ApiController和动作名字获取网址,包含控制器和ApiControllers项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是现有的项目有一个从继承的控制器可以:

An existing project has controllers that inherit from either:


  • 控制器 RouteTable.Routes.MapRoute {控制器} / {行动} / {ID}

  • ApiController GlobalConfiguration.Configure ,并在回调图路线API / {控制器} / {ID}

  • Controller: RouteTable.Routes.MapRoute with "{controller}/{action}/{id}".
  • ApiController: GlobalConfiguration.Configure and in the callback MapRoute with "api/{controller}/{id}".

一切工作正常,但我需要生成在这两种类型的控制器action方法的URL。鉴于:

Everything works fine, but I need to generate URLs for action methods in both of these types of controllers. Given:


  1. 名称或从其中任一继承和控制器的类型

  2. 操作方法名称

再从网站的一面,我怎么能生成Web API侧适当的网址吗?

Then from the web site side, how can I generate proper URLs for the web API side?

我使用反射来获取行动和控制器的名字,现在,然后通过使用 UrlHelper.Action(actionName,controllerName,routeValueDictionary)我得到了网站的路线正确的网址

I'm using reflection to get action and controller names right now, and then through using UrlHelper.Action(actionName, controllerName, routeValueDictionary) am getting the correct URL for web site routes.

不过,这种方法产生这样的网址为的WebAPI侧(当然): / ApiControllerName /获取?参数1 =值时,它需要为 / API / ApiControllerName?参数1 =值和独立的知识,这是一个GET 。要求

However, this method is (of course) generating URLs like this for the WebAPI side: /ApiControllerName/Get?parameter1=value when it needs to be /api/ApiControllerName?parameter1=value and the separate knowledge that it's a GET request.

目的:这是一个使用属性和反射来决定什么烟测试网站烟雾测试页。这将是很好能够使用整个项目相同的属性,而且这将是非常好的能够使用正确的 UrlHelper 这是了解路由表并能产生正确的前缀,如 / API / ,而不是代码假设,或许错误地认为该API的路线,用 API注册而不是,比方说,的WebAPI

Purpose: this is for a smoke test page for the web site that uses attributes and reflection to decide what to smoke test. It would be nice to be able to use the same attribute throughout the project, and furthermore it would be very nice to be able to use the correct UrlHelper that is aware of the routing tables and can produce the right prefix such as /api/, instead of the code assuming, perhaps wrongly, that the API routes were registered with api and not, say, webapi.

更新

继续研究后,我发现的 Url.HttpRouteUrl 方法它可以产生的WebAPI的网址,但是这需要一个了解路由名称,没有一个操作方法的名称。

After continued research I have found the Url.HttpRouteUrl method which can generate WebAPI URLs, but this requires knowing a route name, not an action method name.

我已经做了这甚至更多的研究,并没有得到任何接近的解决方案。它看起来像如果你知道合适的路径的路由名称,您可以轻松地编造一个网址。也有一些可能暗示这里这里。但是,如果有针对的WebAPI多条路线,你怎么知道哪一个你想要的控制器和动作相匹配?这将是愚蠢的重新实现MVC的东西本身就已经在做选择控制器和动作。我想我可以构建从利用一切的WebAPI路线给定参数的URL,然后通过它的步伐运行URL(使用上面的一些链接),看看它是否会匹配所需的控制器......呸。

I've done even more research on this and haven't gotten any closer to a solution. It looks like if you know the route name of the appropriate route, you can cook up a Url easily. There are also some likely hints here and here. But if there are multiple routes for WebApi, how do you know which one matches the controller and action you want? It would be dumb to reimplement what MVC itself already does in selecting a controller and action. I guess I could construct a URL from the given parameters using every WebApi route, then run the URL through its paces (using some of the above links) and see if it is going to match the desired controller... yuck.

有一定是更简单的方法。

There's got to be an easier way.

有关现在我将不得不继续前进,但这里要。希望有人能帮助我

For now I'm going to have to move on, but here's to hoping someone can help me out.

推荐答案

的下面几个方法做到这一点:

Here a few ways of do it:


  • 使用 RouteUrl()方法:

var url1 = Url.RouteUrl(new { id = 1, controller = "...",  httproute = true });


诀窍是当然的 httproute = TRUE 。设置该属性,通知您只需要HTTP路线(网页API路由)。

The trick is of course httproute = true. Setting this property, you inform that you only want http routes (Web Api routes).


  • 使用 HttpRouteUrl()方法:

var url2 = Url.HttpRouteUrl(null, new { id = 2, controller = ".." });


  • 和另一种方法使用直接路由做出来, httproute 值:

    var values = new RouteValueDictionary(new
    {
        id = 3,
        controller = "...",
        httproute = true
    });
    var url3 = RouteTable.Routes.GetVirtualPath(Request.RequestContext, values).VirtualPath;
    


  • 3种方式基本相同。既然你不指定路径,系统将尝试根据路由值找到第一个匹配。例如,假设你有一个 ApiController 名为 RestfulController 和Web API路由配置,像这样:

    The 3 ways are basically the same. Since you don't specify the route, the system will try to find the first match according to the route values. For example, lets say you have an ApiController called RestfulController and web api routes are configured like so:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    



    使用第一种方法,当你做 VAR URL = Url.RouteUrl(新{ID = 123,控制器=平安,httproute =真}); 中的 MVC 控制器,在网址 / API /宁静/ 123

    但是,如果你添加一个新的路线的 ConstraintApi 的:

    But if you add a new route ConstraintApi:

    config.Routes.MapHttpRoute(
        name: "ConstraintApi",
        routeTemplate: "api2/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional },
        constraints: new { controller = "restful" }
    );
    
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    

    RouteUrl 返回的URL / API2 /宁静/ 123

    您应该知道,当你宣布你的路由的顺序。如果路由 DefaultApi 路线 ConstraintApi ,生成的URL是 / API之前添加/宁静/ 123

    You should be aware the order when you are declaring your routes. If the route DefaultApi is added before the route ConstraintApi, the url generated is /api/restful/123.

    这篇关于从ApiController和动作名字获取网址,包含控制器和ApiControllers项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆