How to remove/update default Home page in Azure Mobile Apps

Today, playing around with the Azure Mobile Apps platform, wanted to do some deep customization to the project, one of them: change the Default Blue-styled Home Page. After some research and looking through the Azure Mobile Server GitHub project, i’ve got it done!

For that we’ll be doing these steps:

  • Remove reference to Microsoft.Azure.Mobile.Server.Home
  • Change some lines in Startup.cs (Routes and MobileAppConfiguration initialization)
  • Create a Controller.
  • Create an HTML page as an Embedded Resource

 

First, let’s add our Controller and HTML files

Create an ApiController, maybe called ‘HomeController‘ and put this code:

[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : ApiController
{
     [HttpGet]
     [AllowAnonymous]
     public IHttpActionResult Index()
     {
         return new StaticHtmlActionResult("MyNamespace.WebApi.Home.html");
     }
}

Ref 1

After that, add an HTML page (matching the prefix in the string passed to the StaticHtmlActionResult call) to the main folder of the project and at the Properties tab change the Build Action to Embedded Resources.

Second, to change the Startup.MobileApp.cs file

We need to add a route mapping with our custom Controller and change the actual MobileAppConfiguration implementation (if using the default one):

var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
        name: "Home",
        routeTemplate: string.Empty,
        defaults: new { controller = "Home", action = "Index" });

new MobileAppConfiguration()
             .MapApiControllers()
             .AddTables(
                        new MobileAppTableConfiguration()
                             .MapTableControllers()
                             .AddEntityFramework()
             )
             .ApplyTo(config);

With that, we’re already redirecting our calls to our new HTML page.

Notice that we aren’t using the key extensions method commonly used: AddMobileHomeControllerPage and UseDefaultConfiguration functions.

Finally, let’s remove a Dll reference

If you have been looking the Azure Mobile App’s GitHub project, will notice how the page display is done. All that logic is thanks to few lines in the Microsoft.Azure.Mobile.Server.Home nuget package. Since we already have a reference to it, if we try to run the application we’ll receive an error indicating that ‘Multiple matches found‘. Why? Because config.Routes has two routing matching the same pattern. Fix? Delete the reference and let it be!

You’ll finally be able to show something like this:

custom-azure-mobile-homepage

You can look an example of this implementation in the following GitHub project.

Anuncios