jquery - ASP.NET API Not Found If Not On Main View -
i have apicontroller in simple mvc project (.net 4.5.2, mvc 5.2.3, ef 6.1.3, api 2.2). 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 route default (was added when added api):
public static class webapiconfig { public static void register(httpconfiguration config) { // web api configuration , services // web api routes config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } }
i calling api via ajax on partial view (out-of-the-box _loginpartial.cshtml) 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 login , on main view (start debugging), call api fine. if click on 1 of standard links (again, out-of-the-box template), "not found" message. routing issue , if so, can fix it?
try generate link helper url.action method:
var uri = '@url.action("getcontact", "applicationusercontactapi", new { httproute = "" })';
here's related problem: click!
Comments
Post a Comment