RouteLink到不在MVC 4中使用区域的API控制器

时间:2015-06-04 17:22:48

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-areas asp.net-apicontroller

我喜欢在视图中使用链接指向区域中的ApiController。我尝试了不同类型的注册。我尝试了RouteLink的不同重载。没有成功。无论我如何注册以及如何尝试获取链接,都会返回~/

注册记忆:

        var route1 = context.Routes.MapHttpRoute(
            "IpixDictionary_api1",
            "ipixdic/api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional },
            new[] { "WinGD.AppCenter.Areas.IpixDictionary.Controllers" });

        var route2 = context.Routes.MapHttpRoute(
            "IpixDictionary_api2",
            "ipixdic/api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional },
            new[] { "WinGD.AppCenter.Areas.IpixDictionary.Controllers" });
        if (route2.DataTokens == null)
        {
            route2.DataTokens = new RouteValueDictionary();
        }
        route2.DataTokens.Add("area", context.AreaName);

已使用RouteLink重载:

        @Html.RouteLink("Test11", "IpixDictionary_api1", new { httproute = true, controller = "EnginesApi" })
        @Html.RouteLink("Test12", "IpixDictionary_api1", new { httproute = true, controller = "EnginesApi" }, null)
        @Html.RouteLink("Test13", "IpixDictionary_api1", new { httproute = true, controller = "EnginesApi", area = "IpixDictionary" })
        @Html.RouteLink("Test14", "IpixDictionary_api1", new { httproute = true, controller = "EnginesApi", area = "IpixDictionary" }, null)
        <br />
        @Html.RouteLink("Test21", "IpixDictionary_api2", new { httproute = true, controller = "EnginesApi" })
        @Html.RouteLink("Test22", "IpixDictionary_api2", new { httproute = true, controller = "EnginesApi" }, null)
        @Html.RouteLink("Test23", "IpixDictionary_api2", new { httproute = true, controller = "EnginesApi", area = "IpixDictionary" })
        @Html.RouteLink("Test24", "IpixDictionary_api2", new { httproute = true, controller = "EnginesApi", area = "IpixDictionary" }, null)
        <br />

没有区域,一切正常,但它不会与区域合作。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

找到解决方案。 API控制器在注册中不需要执行操作: - )

以下是我的解决方案:

context.Route.MapHttpRoute(
            "IpixDictionary_api",
            "ipixdic/api/{controller}/{id}",
            new { id = RouteParameter.Optional });

@Html.RouteLink("json", "IpixDictionary_api", new { httproute = true, controller = "EnginesApi" })