ASP.NET MVC 9

Sunday, November 10, 2013

Build run application, in console, mongodb establishes a connection, files are allocated, database is seeded and indexes are created


Controllers folder, add an empty Web Api 2 controller named patientscontroller


Add this code to retrieve all data from mongodb data store


Navigate to api/patients, the seeded data is retrieved and returned in xml format


Query by id, the old web api 1 way of doing this


api/patients/id returns this result in the browser


This method, see route aka attribute routing, returns the medication of the patient by id


Only the mdeciation for that patient is returned to the browser


Web Api 2 IHttpActionResult with helper methods to reduce the code, make it easier to read and make the unit testing require less setup and mocks


Cross origin resource sharing: add a new, empty asp.net project, named corsclient


Add a new html page named default and install jquery from nuget


Open the page in the browser click the button,open developer tools network tab, get back a 404


Cross origin access is not allowed


Install Cors nuget package in main project


And web api 2 cors package


Enable cors in webapiconfig file


Possible to enable cors selectively at the controller level


View Results in browser


Authenticated WebApi, add Authorize attribute at controller level


Copy default.html into main project and rename the h3 header to authentication request

Default Success Warning Important Info Inverse
B S W I ? O