的WebAPI控制器中的MVC路线 [英] MVC Routes within WebAPI controller

本文介绍了的WebAPI控制器中的MVC路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于中MVC和的WebAPI路线快速的问题。我添加了一个路线路线config.cs:

Quick question regarding routes within MVC and WebAPI. I have added a route to route config.cs:

        routes.MapRoute(
            name: "ConfirmEmail",
            url: "ConfirmEmail/{userid}",
            defaults: new { controller = "Email", action = "ConfirmEmail" }
        );

这是登记在Global.asax按正常:

This is registered in the global.asax as per normal:

RouteConfig.RegisterRoutes(RouteTable.Routes);

我想产生哪些形式发来的的WebAPI控制器功能中的一个函数调用的一部分在电子邮件中使用的URL。我使用UrlHelper.Link函数试图生成一个URL,但是我收到一个错误,说的路线不能由名称为:

I am trying to generate a URL for use within an email which is sent as part of a function call within a WebAPI controller function. I am using the UrlHelper.Link function to attempt to generate a URL, however I receive an error saying the route cannot be found by name:

var url = Url.Link("ConfirmEmail", new { userid = "someUserId" });

现在我下的IM pression航线字典两个MVC和的WebAPI控制器上下文,但是我看不到传入的Web API调用的路线字典中的MVC路线(Request对象上)的共享不过的WebAPI我已经定义路线的存在。

Now I was under the impression route dictionaries were shared in both MVC and WebAPI controller contexts however I cannot see the MVC routes within the route dictionary of the incoming Web API call (on the Request object) however the WebAPI routes I have defined are there.

我缺少的东西吗?

推荐答案

有关MVC和Web API的路由表是完全不同的。而语法看起来相似,但它们在操作的路由表是不同的。

The routing tables for MVC and Web API are completely different. While the syntax looks similar, the route table they operate on is different.

不过,MVC使用配置静态对象,所以你可以使用 System.Web.Routing.RouteTable.Routes 的API控制器内访问全局MVC路由表。

However, MVC uses static objects for configuration, so you can access the global MVC route table from within an API controller using System.Web.Routing.RouteTable.Routes.

这不会让无论你使用Url.Link,所以我会建议使用您的注册路线内的常数的格式。

This won't allow you to use Url.Link however, so I would suggest using a constant within your route registration for the format.

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

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