Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am unable to reproduce the problem. This should work. Here's my setup:</p> <ol> <li>Create a new .NET 4.0 Console Application</li> <li>Switch to .NET Framework 4.0 profile</li> <li>Install the <code>Microsoft.AspNet.WebApi.SelfHost</code> NuGet</li> <li><p>Define a <code>Product</code></p> <pre><code>public class Product { public int Id { get; set; } public string Name { get; set; } } </code></pre></li> <li><p>A corresponding API Controller:</p> <pre><code>public class ProductsController : ApiController { public Product Get(int id) { return new Product { Id = id, Name = "prd " + id }; } } </code></pre></li> <li><p>And a host:</p> <pre><code>class Program { static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:8080"); config.Routes.MapHttpRoute( name: "DefaultApiRoute", routeTemplate: "{controller}/{id}.{format}", defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional }, constraints: null ); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } } } </code></pre></li> </ol> <p>Now when you run this console application you could navigate to <code>http://localhost:8080/products/123.xml</code>. But of course you could navigate to <code>http://localhost:8080/products/123.json</code> and you will still get XML. So the question is: How to enable content negotiation using a route parameter?</p> <p>You could do the following:</p> <pre><code> class Program { static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:8080"); config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/html"); config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json"); config.Routes.MapHttpRoute( name: "DefaultApiRoute", routeTemplate: "{controller}/{id}.{ext}", defaults: new { id = RouteParameter.Optional, formatter = RouteParameter.Optional }, constraints: null ); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } } } </code></pre> <p>and now you can use the following urls:</p> <pre><code>http://localhost:8080/products/123.xml http://localhost:8080/products/123.json </code></pre> <p>Now you might be wondering what's the relation between the <code>{ext}</code> route parameter that we used in our route definition and the <code>AddUriPathExtensionMapping</code> method because nowhere we did not specify it. Well, guess what: it's hardcoded in the <code>UriPathExtensionMapping</code> class to <code>ext</code> and you cannot modify it because it is readonly:</p> <pre><code>public class UriPathExtensionMapping { public static readonly string UriPathExtensionKey; static UriPathExtensionMapping() { UriPathExtensionKey = "ext"; } ... } </code></pre> <p>All this to answer your question:</p> <blockquote> <p>Can periods be used in Asp.Net Web Api Routes?</p> </blockquote> <p>Yes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload