从生成的WebAPI路线网址为MVC控制器动作 [英] Generating Route Url to MVC controller action from WebAPI

查看:187
本文介绍了从生成的WebAPI路线网址为MVC控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm used to generating route URLs to other controller actions within an MVC controller action using something similar to below:

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生成的URL MVC控制器动作太大,我怎么会去这样做?

But I also need to be able to generate URLs to MVC controller actions from within webapi too, How would I go about doing this?

有似乎是在一个APIController财产UrlHelper,但我不能找到如何使用任何的例子,并没有能够找出自己。

There seems to be a UrlHelper property on the APIController but I cant find any examples of how to use this and have not been able to figure it out myself.

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

UPDATE : The reason I am trying to generate a url is that this particular webapi method sends an email which provides the recipient with a link to direct the user back to an appropriate section of the site. I obviously want to get away from hardcoding this as it will not work for different deployments and also if I begin changing the routing this link will be broken. Is there a better approach to doing this?

推荐答案

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

You can use MVC route names as well with web API UrlHelper. Example,

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

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

这篇关于从生成的WebAPI路线网址为MVC控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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