使用WEB API和MVC5中的区域进行URL路由

Ank*_*ain 11 asp.net-mvc routing url-routing asp.net-mvc-4 asp.net-mvc-5

我正在研究MVC 5应用程序.在那我已经创建了一个名为的区域Organization.我还在该区域的controllers文件夹中创建了一个(API)文件夹.所以我的结构变得像

/Areas/Organization/Controllers/API/OrganizationAPI
Run Code Online (Sandbox Code Playgroud)

区域的OrganizationAPIAPI控制器在哪里Organization.现在我的问题是关于MVC中的路由.我无法找到将调用我的API的URL.

我试过这个URL

http://localhost:80/Organization/API/OrganizationAPI/getData
Run Code Online (Sandbox Code Playgroud)

getData我的行动方法在哪里.但它说没有找到资源.有什么可以帮助我理解我如何注册我自己的路由,以便我可以使用URL映射我的操作,并建议我在MVC 4或更高版本中的URL路由的一些参考URL.

Jas*_*lia 9

如果您的项目已包含WebApiConfig.cs文件,那太好了!您只需添加如下路线:

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

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // NEW ROUTE FOR YOUR AREA
        config.Routes.MapHttpRoute(
            name: "API Area Default",
            routeTemplate: "api/AREA/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)