How to use controllers to access data in mobile services (.NET backend)
[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]
To store and work with data in .NET mobile services, you create a data object that has the fields you want, and then you create a table controller which provides the interface for clients to work with that data. A table controller is a class in a .NET mobile services project that exposes basic data operations as HTTP methods. The HTTP interface is the same as a Web API controller, with HTTP methods for all the basic operations. By following the procedures in this topic, you create a data object and a table controller for clients who use your mobile service to create, read, update, and delete data. You don't have to write the Web API controller code, since Visual Studio has templates that provide default implementations for the most common operations. This default code is a scaffold, meaning that it provides a simple implementation, which you can then modify as needed for your scenario.
This topic applies to mobile services that use the .NET backend. If you're using the JavaScript backend, see How to use scripts to access data in mobile services (JavaScript backend).
Prerequisites
- Windows 8.1
- Microsoft Visual Studio 2013
- Microsoft Visual Studio 2013 Update 2
- An Azure subscription. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.
- A .NET mobile service project in C# or Visual Basic. If you don't have one, follow this quickstart: Quickstart: Add a mobile service (.NET backend).
Instructions
Step 1: Create a data object
In Solution Explorer, choose the DataObjects node, open the shortcut menu in the Visual Studio project, and choose Add New Item. Choose the Class item template, which adds an empty class.
Add this using statement to the top of the file.
using Microsoft.WindowsAzure.Mobile.Service;
Modify the code for the class to make the base class EntityData.
public class MyDataObjectClass : EntityData
The base class EntityData provides everything needed for mobile services to use the class in a table controller. It's part of the Entity Framework. See Entity Framework.
Add public properties to the class to suit your needs. These will become the columns in the data table, which is created for you in the database. The properties should be auto-properties. Do not implement a get or set method.
public string Name { get; set; } public int Count { get; set; }
Step 2: Add a table controller
In Solution Explorer, open the shortcut menu and choose Add > New Scaffolded Item. The Add Controller dialog box appears.
Note A scaffold is a controller class which provides default implementations for operations on your data.
Choose Microsoft Azure Mobile Services Table Controller. The Add Controller dialog box appears.
For Model class, choose your data object from the dropdown list.
For Data context class, choose the existing context class.
Choose Add. A file is added to your project that contains the code for the controller, with several built-in HTTP methods.
Step 3: Run the mobile service locally
Entity Framework automatically updates the database schema for the new table, but those changes don't take place until you actually use the table. In this step, you run the mobile service on the local computer and submit a POST to it.
If you have an open connection to the localdb database that your mobile services uses, close it. In Server Explorer, choose the node for the database, choose Close Connection. If you don't do this, the new table won't be created successfully since it requires an exclusive lock on the database.
In Solution Explorer, choose the mobile service project node, open the short cut menu, and choose Set As Startup Project.
Press F5 or choose Start Debugging from the Debug menu. The mobile service starts, and a browser window appears with a test page for your service.
Choose the try it out link.
The API Documentation page appears. This page lets you test the mobile service's APIs.
Locate the name of the new table in the list, and choose the link for the POST method for your table.
Click the try this out link.
Edit the request and click on the send button.
At this time, Entity Framework adds the table to the database and the service sends a response.
Close the browser window.
Reopen the connection to the database by opening the shortcut menu for the database in Server Explorer, and choosing Modify Connection. Then choose the OK button. Expand the Tables node and see the new table. If you don't see the table, refresh the data by opening the shortcut menu for the database node, and choosing Refresh.
Step 4: Set permissions on the controller methods
By default, an anonymous Internet user cannot access a mobile service methods unless they have the application key. This procedure shows how to change the authorization. You can use the RequiresAuthorization attribute to enable everyone to access the controller's methods, or to restrict permissions to authenticated users or administrators. See Get started with authentication in Mobile Services. Valid values of the AuthorizeLevel enumeration are Admin (administrators only), Anonymous (no restrictions), Application (requires the application key), and User (requires authenticated credentials).
Warning Using the Anonymous setting is a potential security risk. You shouldn't grant greater access than your app requires.
Open the TodoItemController.cs or .vb file.
Add the following using statement.
using Microsoft.WindowsAzure.Mobile.Service.Security;
Imports Microsoft.WindowsAzure.Mobile.Service.Security
Add the following attribute on the controller class, or in front of each method that you want to set the permissions on.
[AuthorizeLevel(AuthorizationLevel.Anonymous)]
<AuthorizeLevel(AuthorizationLevel.Anonymous)>