MVC & Web Api projects within same Solution

后端 未结 3 666
余生分开走
余生分开走 2021-02-02 11:28

I have an MVC 4 project sitting atop an N-tier application. I now have a requirement to to be able to consume the application programmatically. I have created a new Web API proj

3条回答
  •  眼角桃花
    2021-02-02 11:39

    I just did the same thing yesterday. I have in the same MVC 4 project regular Controllers and ApiControllers.

    You need to add the routing in the Global Asax for WebApi :

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    

    Take a look at the WebApiConfig :

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
    

    Don't forget also to add the Nuget Packages for WebApi (if you don't have them already). In my case I did not had them because my project was originally MVC 3 and was later upgraded.

提交回复
热议问题