I have an ApiController that is in my simple MVC project (.Net 4.5.2, MVC 5.2.3, EF 6.1.3, API 2.2). It is shown here:
namespace MyApplication.PM.Controllers.API
{
public class ApplicationUserContactAPIController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: api/ApplicationUserContactAPI/5
[ResponseType(typeof(Contact))]
public async Task<IHttpActionResult> GetContact(Guid id)
{
Contact contact = await db.Contacts.SingleOrDefaultAsync(c => c.ApplicationUserID == id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
}
}
}
Registering the route by default (was added when I added an API):
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I am calling this API via ajax on my partial view (out-of-the-box _LoginPartial.cshtml) like so:
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
var uri = 'api/ApplicationUserContactAPI';
var userid = '@User.Identity.GetUserId()';
$(document).ready(function () {
$.getJSON(uri + '/' + userid)
.done(function (data) {
$('.manageAccount').text('Hello ' + data.FirstName + '!');
})
.fail(function (jqXHR, textStatus, err) {
alert('error: ' + err);
});
});
</script>
If I login and I am on my main view (start debugging), the call to the API is fine. If i click on one of the standard links (again, out-of-the-box from template), I get a "Not Found" message. Is this a routing issue and if so, what can I do to fix it?