title: Web Api Post error -> Value cannot be null. Parameter name: uriString
body: <p>I am relatively new to Web Api and I am having trouble POSTing a Person object.  If I run in debug, I see that my uriString never gets set and I don't understand why.  Because of this, I get "400 Bad Request" errors in Fiddler for all attempted Posts. </p>
  
  <p>I have tried replicating what others have done when it comes to the Post action.  Every example I've found uses a repository to add the person to the database.  I do not have repositories however, but instead am using the NHibernate Save method to carry out this functionality.  Here are the domain class, mapping by code file, WebApiConfig, and the PersonController.</p>
  
  <pre><code>public class Person
  {
      public Person() { }
  
      [Required]
      public virtual string Initials { get; set; }
      public virtual string FirstName { get; set; }
      public virtual char MiddleInitial { get; set; }
      public virtual string LastName { get; set; }
  }
  
  public class PersonMap : ClassMapping&lt;Person&gt;
  {
      public PersonMap() 
      {
          Table("PERSON");
          Lazy(false);
  
          Id(x =&gt; x.Initials, map =&gt; map.Column("INITIALS"));
  
          Property(x =&gt; x.FirstName, map =&gt; map.Column("FIRST_NAME"));
          Property(x =&gt; x.MiddleInitial, map =&gt; map.Column("MID_INITIAL"));
          Property(x =&gt; x.LastName, map =&gt; map.Column("LAST_NAME"));  
      }
  }
  
  
  
  public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          var json = config.Formatters.JsonFormatter;
          json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
          config.Formatters.Remove(config.Formatters.XmlFormatter);
  
          config.Services.Replace(typeof(IHttpActionSelector), new HybridActionSelector());
  
  
  
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}/{action}/{actionid}/{subaction}/{subactionid}",
              defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional,
                              actionid = RouteParameter.Optional, subaction = RouteParameter.Optional, subactionid = RouteParameter.Optional }
          );
  
  
          config.BindParameter( typeof( IPrincipal ), new ApiPrincipalModelBinder() );
  
          // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable&lt;T&gt; return type.
          // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
          // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
          //config.EnableQuerySupport();
  
          // To disable tracing in your application, please comment out or remove the following line of code
          // For more information, refer to: http://www.asp.net/web-api
          config.EnableSystemDiagnosticsTracing();
      }
  }
  
  
  
  public class PersonsController : ApiController
  {
      private readonly ISessionFactory _sessionFactory;
  
      public PersonsController (ISessionFactory sessionFactory)
      {
          _sessionFactory = sessionFactory;
      }
  
      // POST api/persons
      [HttpPost]
      public HttpResponseMessage Post(Person person)
      {
          var session = _sessionFactory.GetCurrentSession();
  
          using (var tx = session.BeginTransaction())
          {
              try
              {
                  if (!ModelState.IsValid)
                  {
                      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                  }
  
                  var result = session.Save(person);
                  var response = Request.CreateResponse&lt;Person&gt;(HttpStatusCode.Created, person);
  
                  string uriString = Url.Route("DefaultApi", new { id = person.Initials });
                  response.Headers.Location = new Uri(uriString); 
  
  
                  tx.Commit();
                  return response;
              }
              catch (Exception)
              {
                  tx.Rollback();
              }
              throw new HttpResponseException(HttpStatusCode.BadRequest);
          }
      }
  }
  </code></pre>
  
  <p>Fiddler information:
     POST  //localhost:60826/api/employees    HTTP/1.1</p>
  
  <p>Request Headers:
     User-Agent: Fiddler
     Content-Type: application/json
     Host: localhost:xxxxx
     Content-Length: 71</p>
  
  <p>Request Body:</p>
  
  <p>{
       "Initials":"MMJ",
       "LastName":"Jordan",
       "FirstName":"Michael"
     }</p>
  
  <p>This line never sets the uriString to the correct value.                     string uriString = Url.Route("DefaultApi", new { id = person.Initials });
   I've also tried using Url.Link instead of Url.Route.  I've tried adding the controller = "Persons" inside the 'new' block, but that had no effect.  Why isn't uriString being set?  I'll listen to any thoughts at this point.</p>
  
  <p><strong>EDIT</strong>
  I have tried </p>
  
  <pre><code>string uriString = Url.Link("DefaultApi", new { controller = "Persons", id = person.Initials, action="", actionid="", subaction="", subactionid="" });
  </code></pre>
  
  <p>as well as using a separate routeconfig</p>
  
  <pre><code>config.Routes.MapHttpRoute(
              name: "PostApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional
          } );
  </code></pre>
  
  <p>with</p>
  
  <pre><code>string uriString = Url.Link("PostApi", new { controller = "Persons", id = person.Initials});
  </code></pre>
  
  <p>and have had no luck.</p>
  
  <p><em><strong>SOLUTION</em></strong></p>
  
  <p>I was able to get this Post to work by using the line of code below.  I'm not entirely sure if this is the correct way to do it, so if anybody knows differently, please share.  Otherwise, I will happily use this approach.</p>
  
  <pre><code>response.Headers.Location = new Uri(this.Request.RequestUri.AbsoluteUri + "/" + person.Initials);
  </code></pre>