共用方式為


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

Instructions

Step 1: Create a data object

  1. 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.

  2. Add this using statement to the top of the file.

    using Microsoft.WindowsAzure.Mobile.Service;
    
  3. 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.

  4. 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

  1. 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.

     

  2. Choose Microsoft Azure Mobile Services Table Controller. The Add Controller dialog box appears.

  3. For Model class, choose your data object from the dropdown list.

  4. For Data context class, choose the existing context class.

  5. 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.

  1. 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.

  2. In Solution Explorer, choose the mobile service project node, open the short cut menu, and choose Set As Startup Project.

  3. 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.

  4. Choose the try it out link.

    The API Documentation page appears. This page lets you test the mobile service's APIs.

  5. Locate the name of the new table in the list, and choose the link for the POST method for your table.

  6. Click the try this out link.

  7. 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.

  8. Close the browser window.

  9. 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.

 

  1. Open the TodoItemController.cs or .vb file.

  2. Add the following using statement.

    using Microsoft.WindowsAzure.Mobile.Service.Security;
    
    Imports Microsoft.WindowsAzure.Mobile.Service.Security
    
  3. 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)>
    

Add a Mobile Service

Add a Custom API

Work with data in mobile services