获得一个ApiController与地区工作? [英] Getting an ApiController to work with areas?

查看:96
本文介绍了获得一个ApiController与地区工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有我的ASP .NET MVC 5项目2个区域。一个是所谓的的支持者的,还有一个叫看点的。在每一个这两个领域,有一个 ApiController 名为 CommunicationController ,这提出了一个问题,由于性质如何在 ApiController 适用于路由。

I currently have 2 areas in my ASP .NET MVC 5 project. One is called Supporters, and one is called Chatter. In each of these two areas, there is an ApiController named CommunicationController, and this poses a problem due to the nature of how an ApiController works with routing.

如果我刚一 ApiController 名为 CommunicationController 在一个地区,它的路由将不包括区域中的URL和URL会是这样的:

If I had just one ApiController named CommunicationController in an area, its routing wouldn't include the area in the URL, and the URL would be something like this:

http://example.com/api/communication/someAction

但在上述网址,这里是什么地方呢?

But in the above URL, where is the area?

由于我的两个控制器的命名一样,他们俩现在有路由问题。

Since two of my controllers are named the same, they both have routing issues now.

我想这里的操作说明:的 http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely /

I tried following the instructions here: http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/

他们似乎适用于ASP .NET MVC 4 RC,它不再是相关的,因为我使用MVC 5,这可能是为什么不能正常工作。

They seem to apply for ASP .NET MVC 4 RC, which is no longer relevant since I am using MVC 5, and that might be why it wasn't working.

不过,从该博文回顾一下,这里有我的路由文件。

However, to recap from that blogpost, here are my routing files.

< STRONG> App_Start\RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("Default", "{controller}/{action}",
            new {action = "Index", controller = "Home"},
            new[] { "Website.Controllers" }
            );
    }
}



App_Start\WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        //the two lines below were added.
        config.Routes.MapHttpRoute("SupportersApi", "api/supporters/{controller}/{id}", new {id = RouteParameter.Optional, area = "Supporters"}
            );
        config.Routes.MapHttpRoute("ChatterApi", "api/chatter/{controller}/{id}", new { id = RouteParameter.Optional, area = "Chatter" }
            );
    }
}



Areas\Chatter\ChatterAreaRegistration。 CS

public class ChatterAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Chatter"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {

        //the below line was added.
        context.Routes.MapHttpRoute("Chatter_api", "api/chatter/{controller}/{id}", new { id = RouteParameter.Optional, area = "Chatter" }
            );

        context.MapRoute(
            "Chatter_default",
            "Chatter/{controller}/{action}/{id}",
            new {action = "Index", id = UrlParameter.Optional}
            );
    }
}



Areas\Supporters\SupportersAreaRegistration。 CS

public class SupportersAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Supporters";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        //the below line was added.
        context.Routes.MapHttpRoute("Supporters_api", "api/supporters/{controller}/{id}", new { id = RouteParameter.Optional, area = "Supporters" }
            );

        context.MapRoute(
            "Supporters_default",
            "Supporters/{controller}/{action}/{id}",
            new { action = "Index", controller = "Home", id = UrlParameter.Optional }
        );
    }
}



我在做什么错在这里,什么是我的选择?

What am I doing wrong here, and what are my options?

推荐答案

使用的的WebAPI 2属性的,因为你正在使用MVC 5,你可以摆脱了很多的样板代码(/序列化的最月你也可以指定HTTP动词的动作,甚至使用属性来自动转换成XML / JSON)由与它一起宣布的路线为API的实现。

Use the WebAPI 2 attributes, since you are using MVC 5, and you can get rid of a lot of that boilerplate code by declaring the routes for your API along with it's implementation (you can also specify verbs for HTTP actions, and even use attributes to auto-convert to XML/JSON/serialization-of-the-month).

除非你使用领域某些其他原因,你真的不需要他们来实现Web API。

Unless you are using areas for some other reason, you really don't need them to implement a Web API.

在特定的,你想要什么RoutePrefix属性。

In particular, what you want is the RoutePrefix attribute.

这篇关于获得一个ApiController与地区工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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