Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:14:01+00:00 2026-06-07T16:14:01+00:00

I’m working on moving an API project from raw http handlers where I’m using

  • 0

I’m working on moving an API project from raw http handlers where I’m using periods in the paths:

http://server/collection/id.format

I would like to follow the same URL schema in a Web Api (self-hosted) version, and tried this:

var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
    name: "DefaultApiRoute",
    routeTemplate: "{controller}/{id}.{format}",
    defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
    constraints: null
);

Unfortunately, that doesn’t seem to resolve (consistent 404’s on /foo, /foo/bar and /foo/bar.txt). A similar pattern using a slash before ‘format’ works fine:

var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
    name: "DefaultApiRoute",
    routeTemplate: "{controller}/{id}/{format}",
    defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
    constraints: null
);

I haven’t yet delved into the code for the Web Api, and before I do thought I’d ask here to see if this is a known, or perhaps even justified limitation in Web Api.

UPDATE: I neglected to mention that “id” and “format” are strings, which turns out to be important for the solution to this question. Adding a constraint to exclude periods from the “id” token solves the 404 problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:14:04+00:00Added an answer on June 7, 2026 at 4:14 pm

    I am unable to reproduce the problem. This should work. Here’s my setup:

    1. Create a new .NET 4.0 Console Application
    2. Switch to .NET Framework 4.0 profile
    3. Install the Microsoft.AspNet.WebApi.SelfHost NuGet
    4. Define a Product

      public class Product
      {
          public int Id { get; set; }
          public string Name { get; set; }
      }
      
    5. A corresponding API Controller:

      public class ProductsController : ApiController
      {
          public Product Get(int id)
          {
              return new Product
              {
                  Id = id,
                  Name = "prd " + id
              };
          }
      }
      
    6. And a host:

      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();
              }
          }
      }
      

    Now when you run this console application you could navigate to http://localhost:8080/products/123.xml. But of course you could navigate to http://localhost:8080/products/123.json and you will still get XML. So the question is: How to enable content negotiation using a route parameter?

    You could do the following:

        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();
                }
            }
        }
    

    and now you can use the following urls:

    http://localhost:8080/products/123.xml
    http://localhost:8080/products/123.json
    

    Now you might be wondering what’s the relation between the {ext} route parameter that we used in our route definition and the AddUriPathExtensionMapping method because nowhere we did not specify it. Well, guess what: it’s hardcoded in the UriPathExtensionMapping class to ext and you cannot modify it because it is readonly:

    public class UriPathExtensionMapping
    {
        public static readonly string UriPathExtensionKey;
    
        static UriPathExtensionMapping()
        {
            UriPathExtensionKey = "ext";
        }
    
        ...
    }
    

    All this to answer your question:

    Can periods be used in Asp.Net Web Api Routes?

    Yes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.