ASP.NET的Web API使用Url.Action生成的url [英] ASP.NET Web API generate url using Url.Action

查看:472
本文介绍了ASP.NET的Web API使用Url.Action生成的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能产生相同的URL,但在网页API?

  VAR URL = Url.Action(行动,控制器,新产品{= product.Id,价格=价格},协议:Request.Url.Scheme);

P.S。

URL应该产生一个MVC控制器/行动,但来自网络API中。

所以基本上:做一个GET请求我的 API / generateurl ,这将返回一个网址:

  http://domain.com/controller/action?product=productId&price=100


解决方案

也许最接近帮手Url.Action在网页API控制器是Url.Link方法,将生成路由名称对应的URL,控制器名称,操作名称和路线参数(如果需要)。

下面是一个简单的例子

默认App_start / RouteConfig.cs

  routes.MapRoute(
    名称:默认,
    网址:{控制器} / {行动} / {ID}
    默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);

在Web API控制器:

 公共类MyWebApiController:ApiController
{
    公共字符串获得()
    {
        VAR URL = this.Url.Link(默认,新{控制器=MyMvc,行动=MyAction,参数1 = 1,参数2 =somestring});
        返回URL;
    }
}

的MVC控制器

 公共类MyMvcController:控制器
{
    公众的ActionResult MyAction(INT参数1,字符串参数2)
    {
        // ...
    }
}

由控制器的WebAPI生成的URL将是的http:// myDomain的/ MyMvc / MyAction参数1 = 1&安培;参数2 = somestring

我没有找到如何通过协议/ URL架构,但在这将​​是只是一个字符串,你可以操纵它,如果你知道这个协议应该是什么。

希望这有助于。

编辑:

这可能有助于该协议的一部分:
<一href=\"http://stackoverflow.com/questions/24247402/generate-https-link-in-web-api-using-url-link\">Generate使用Url.Link 在网页API HTTPS链接

How can I generate the same url but in Web Api ?

var url = Url.Action("Action", "Controller", new { product = product.Id, price = price }, protocol: Request.Url.Scheme);

P.S.

The url should be generated to an MVC controller/action but from within web api.

So basically: make a get request to my api/generateurl and that will return an url to :

http://domain.com/controller/action?product=productId&price=100

解决方案

Maybe the closest helper to Url.Action in Web Api Controller is the Url.Link method which will generate the url by Route name, Controller Name, Action Name and the route parameters (if needed).

Here is a simple example

The default App_start/RouteConfig.cs

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The Web Api Controller:

public class MyWebApiController : ApiController
{
    public string Get()
    {
        var url = this.Url.Link("Default", new { Controller = "MyMvc", Action = "MyAction", param1 = 1, param2 = "somestring" });
        return url;
    }
}

The MVC Controller

public class MyMvcController : Controller
{
    public ActionResult MyAction(int param1, string param2)
    {
        // ...
    }
}

The generated url by the WebApi controller will be http://myDomain/MyMvc/MyAction?param1=1&param2=somestring.

I didn't find how to pass the protocol/url schema but at the and it will be just a string and you can manipulate it if you know what the protocol should be.

Hope this helps.

EDIT:

This may help for the protocol part: Generate HTTPS link in Web API using Url.Link

这篇关于ASP.NET的Web API使用Url.Action生成的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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