How to use MVC controller and WebAPI controller in the same project

I am trying to use MVC controller and WebAPI controller in one project, but I got 404 errors for webapi. I started the project as MVC Project in vs 2015, but then added a webapi control, and with the default code it throws a 404 error

what could be a possible solution. I tried some solution on Stackoverflow, but they did not work, one of them is below the link (Accepted there). All ASP.NET Web API controllers return 404

Global.ASAX File Code:

protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } 

WEBAPI.CONFIG FILE

 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 } ); } } 

Route Configuration File Code

  public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 

WebAPI Controller Code

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using O365_APIs_Start_ASPNET_MVC.Models; using Microsoft.IdentityModel.Clients.ActiveDirectory; using O365_APIs_Start_ASPNET_MVC.Helpers; using System.Threading.Tasks; namespace O365_APIs_Start_ASPNET_MVC.Controllers { public class MAILAPIController : ApiController { private MailOperations _mailOperations = new MailOperations(); //async Task<BackOfficeResponse<List<Country>>> // GET: api/MAILAPI public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET: api/MAILAPI/5 public string Get(int id) { return "value"; } // POST: api/MAILAPI public void Post([FromBody]string value) { } // PUT: api/MAILAPI/5 public void Put(int id, [FromBody]string value) { } // DELETE: api/MAILAPI/5 public void Delete(int id) { } } } 

ALSO GETTING A NUGET RECOVERY ERROR IN THE SAME SOLUTION Nuget failed to repair PNG

+12
c # asp.net-mvc asp.net-web-api asp.net-mvc-4
source share
4 answers

You need to register the routing for the web api BEFORE registering the routing for the MVC, so basically your App_Start() function should look like this:

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd BundleConfig.RegisterBundles(BundleTable.Bundles); } 
+21
source share

Just add one line to the Application_Start () method of the asax global page.

enter image description here

0
source share

You have two routes that are displayed the same way. Either delete delete (id) or get (id). In addition, you can add an action to your route.

0
source share

you can write both configurations in one file like this

 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 

in the Global.aspx file, edit the code, for example this--

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(RouteConfig.Register);//WEB API 1st FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd BundleConfig.RegisterBundles(BundleTable.Bundles); } 
0
source share

All Articles