title: MVC Controller unable to find Api Controller (same project)
body: <p>(Sorry if my english sucks a little)</p>
  
  <p>I'm trying to call an api method from a mvc controller but the mvc seems unable to find the method. I set the route in the mvc controller as </p>
  
  <p><code>[Route("[controller]")]</code></p>
  
  <p>and in the api controller as</p>
  
  <p><code>[Route("api/[controller]")]</code></p>
  
  <p>In the startup.cs file i added this command to enable default route</p>
  
  <p><code>app.UseMvcWithDefaultRoute();</code></p>
  
  <p>Mvc controller code:</p>
  
  <pre><code>[HttpGet]
      public async Task&lt;ActionResult&gt; GetAll()
      {
          IEnumerable&lt;Utente&gt; utenti = null;
  
          using (var client = new HttpClient())
          {
              client.BaseAddress = new Uri("http://localhost:57279/");
              var Res = await client.GetAsync("api/utente/GetAll");
  
              if (Res.IsSuccessStatusCode)
              {
                  var readTask = Res.Content.ReadAsAsync&lt;IList&lt;Utente&gt;&gt;();
                  utenti = readTask.Result;
              }
              else
              {
                  utenti = Enumerable.Empty&lt;Utente&gt;();
  
                  ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
              }
          }
          return View(utenti);
      }
  </code></pre>
  
  <p>Api code:</p>
  
  <pre><code>[HttpGet]
      public IHttpActionResult GetAll()
      {
          IList&lt;Utente&gt; utenti = null;
  
          using (_utenteContext)
          {
              utenti = _utenteContext.Utenti.Select(u =&gt; new Utente()
                          {
                              id = u.id,
                              user = u.user,
                              password = u.password
                          }).ToList&lt;Utente&gt;();
          }
  
          if (utenti.Count == 0)
          {
              return NotFound();
          }
  
          return Ok(utenti);
      }
  </code></pre>
  
  <p>The problem might be that I'm following an old example for both mvc and api controllers in same project, but I'd like if you guys could help me with it.</p>
  
  <p>In the:</p>
  
  <p><code>var Res = await client.GetAsync("api/utente/GetAll");</code></p>
  
  <p>I always get {StatusCode: 404, ReasonPhrase: 'Not Found',...} no matter the changes I make to the code.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Whole Api Controller (I was trying also with a POST method but it doesn't work either)</p>
  
  <pre><code>using AdrianWebApi.Models;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Http;
  
  namespace AdrianWebApi.Controllers.api
  {
  [Route("api/[controller]")]
  public class UtenteController : ApiController
  {
      private readonly UtenteContext _utenteContext;
  
      public UtenteController(UtenteContext context)
      {
          _utenteContext = context;
      }
  
      [HttpGet]
      public IHttpActionResult GetAll()
      {
          IList&lt;Utente&gt; utenti = null;
  
          using (_utenteContext)
          {
              utenti = _utenteContext.Utenti.Select(u =&gt; new Utente()
                          {
                              id = u.id,
                              user = u.user,
                              password = u.password
                          }).ToList&lt;Utente&gt;();
          }
  
          if (utenti.Count == 0)
          {
              return NotFound();
          }
  
          return Ok(utenti);
      }
  
      [HttpPost]
      public IHttpActionResult PostNewUtente(Utente utente)
      {
          if (!ModelState.IsValid)
              return BadRequest("Not a valid model");
  
          using (_utenteContext)
          {
              _utenteContext.Utenti.Add(new Utente()
              {
                  id = utente.id,
                  user = utente.user,
                  password = utente.password
              });
  
              _utenteContext.SaveChanges();
          }
  
          return Ok();
      }
  }
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong>
  Startup class if it's useful:</p>
  
  <pre><code>using AdrianWebApi.Models;
  using AdrianWebApi.Models.DataManager;
  using AdrianWebApi.Models.Repository;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  
  namespace AdrianWebApi
  {
  public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;UtenteContext&gt;(options =&gt;{options.UseMySQL("server=localhost;database=dbutenti;User ID=root;password=root;");});
          services.AddScoped&lt;IDataRepository&lt;Utente&gt;, DataManager&gt;();
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          app.UseMvcWithDefaultRoute();
      }
  }
  }
  </code></pre>
  
  <p><strong>EDIT 3</strong> Post method MVC if someone is interested, working, at least for me:</p>
  
  <pre><code>[Route("Add")]
      [System.Web.Http.HttpPost]
      public ActionResult Add([FromForm]Utente utente)
      {
          if (utente.password == null)
          {
              return View();
          }
          else
          {
  
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri("http://localhost:57279/api/");
  
                  //HTTP POST
                  var postTask = client.PostAsJsonAsync&lt;Utente&gt;("utente", utente);
                  postTask.Wait();
  
                  var result = postTask.Result;
                  if (result.IsSuccessStatusCode)
                  {
                      return RedirectToAction("GetAll");
                  }
              }
              ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
              return View(utente);
          }
      }
  </code></pre>