How to access localhost from Android emulator and iOS simulator?

Being lucky to develop a backend and a Xamarin.Forms clients on your own? Sooner or later you will have to debug the API calls and it might become painful. Unless, you will follow the next rules:

  1. Configure your API URL to run on instead of a localhost:

    // .NET Core Web.Api example
    public static IWebHost BuildWebHost(string[] args) =>

  2. Configure your Xamarin.Forms API consumer to have a conditional URL base:

    string apiUrl = null;
    if (Device.RuntimePlatform == Device.Android)
    apiUrl = ““;
    else if (Device.RuntimePlatform == Device.iOS)
    apiUrl = “http://localhost:5001/api“;
    throw new UnsupportedPlatformException();

The problem with Android emulator is that it maps to, not to localhost. However, the iOS Simulator uses the host machine network.

That should be it!
Happy debugging!

Web.API 2 centralised model state validation

Model state validation is easy with DataAnnotations. However, you will find repeating the next line of code very soon:

if(!ModelState.IsValid) return BadRequest(..);

Luckily, there is a pretty simple solution – to use ActionFilter:

using System;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace MyAwesomeWebApi.ActionFilters
/// <summary>
/// Returns 400 if the ModelState is invalid.
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public class ValidateModelAttribute : ActionFilterAttribute
public override void OnActionExecuting(HttpActionContext actionContext)
if (actionContext.ModelState.IsValid)
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using System.ComponentModel.DataAnnotations;
namespace MyAwesomeWebApi.Controllers
public class ValuesController : ApiController
public IHttpActionResult Post(MyModel model)
var valService = new ValService();
var result = valService.Create(model);
return Created($"api/values/{result.Id}", result);
public class MyModel
public int Id { get; set; }
view raw ValuesController.cs hosted with ❤ by GitHub

All you have to do is to add the ValidateModel attribute to your controller methods that require model state validation.