创建从另一个控制器到控制器的URL链接 [英] Create url link to a controller from another controller

查看:55
本文介绍了创建从另一个控制器到控制器的URL链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET Core 2.0中开发了WebApi RESTful.我想从控制器A中的操作方法中创建位于其他控制器B中的操作方法的URL地址:

I develop a WebApi RESTful in ASP.NET Core 2.0. From an action method in a controller A I want to create the URL address for an action method located in a different controller B:

[Route("api/applications/{idbcon}/bcontrollers")]
public class BController : Controller{
    [HttpGet({idbcon}), Name = "ActionB"]
    public IActionResult ActionB(int idbcon, [FromHeader(Name = "Accept")] string mediatype){
        return OK();
    }
}


[Route("api/applications/{idapp}/bcontrollers/{idbcon}/acontrollers")]
public class AController: Controller{
    [HttpGet(), Name = "GetURLBController"]
    public IActionResult GetURLBController()
    {
        var url = /* Here I would like to get the link to "ActionB" that belong to a Controller "BController" */;
        return Ok(url);
    }
}

任何建议???

推荐答案

使用

IUrlHelper 是您从 Controller 基类继承的东西,显示为 Url 属性.

IUrlHelper is something you inherit from the Controller base class, exposed as the Url property.

这篇关于创建从另一个控制器到控制器的URL链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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