-->

从产生的WebAPI路线的URL MVC控制器动作(Generating Route Url to

2019-07-21 02:55发布

我已经习惯了使用类似下面生成MVC控制器动作中路由指向其他控制器操作:

public class ApplicationController : Controller
{
    public ActionResult Index( )
    {
        var url = Url.RouteUrl("routename",
           new { controller = "Application", Action = "Index2" , other="other" });
    }

    public ActionResult Index2( string other)
    {
    }
}

但我也需要能够产生从内部的WebAPI过的网址MVC控制器动作,我将如何去这样做呢?

人们似乎对APIController一个UrlHelper财产,但我不能找到如何利用这一点,并没有能够找出自己的任何实例。

更新:我想生成一个URL的原因是,这个特殊的WebAPI方法将其提供与链接收件人引导用户回到网站的相应部分的电子邮件。 我显然想从这个硬编码离开,因为它不会对不同的部署工作,另外,如果我开始改变路由此链接将被打破。 是否有这样做的更好的方法?

Answer 1:

您可以使用Web API UrlHelper使用MVC路线名称为好。 例,

 Url.Link("Default", new { Controller = "Account", Action = "Login" });

要么

 Url.Route("Default", new { Controller = "Account", Action = "Login" });


文章来源: Generating Route Url to MVC controller action from WebAPI