Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287

The URL I'm trying to let work is one in the style of: http://somedomain.com/api/people/staff.33311 (just like sites as LAST.FM allow all sort of signs in their RESTFul & WebPage urls, for example "http://www.last.fm/artist/psy'aviah" is a valid url for LAST.FM).

What works are following scenarios: - http://somedomain.com/api/people/ - which returns all people - http://somedomain.com/api/people/staff33311 - would work as well, but it's not what I'm after I'd want the url to accept a "dot", like the example below - http://somedomain.com/api/people/staff.33311 - but this gives me a

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 

I've set up following things:

  1. The controller "PeopleController"

    public IEnumerable<Person> GetAllPeople() {     return _people; }  public IHttpActionResult GetPerson(string id) {     var person = _people.FirstOrDefault(p => p.Id.ToLower().Equals(id.ToLower()));     if (person == null)         return NotFound();      return Ok(person); }     
  2. The WebApiConfig.cs

    public static void Register(HttpConfiguration config) {     // Web API configuration and services      // Web API routes     config.MapHttpAttributeRoutes();      config.Routes.MapHttpRoute(         name: "DefaultApi",         routeTemplate: "api/{controller}/{id}",         defaults: new { id = RouteParameter.Optional }     ); } 

I already tried following all the tips of this blogpost http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx but it still won't work.. I also think it's quite tedious and I wonder if there isn't another, better and more secure way.

We have our Id's internally like this, so we're going to have to find a solution to fit the dot in one way or another, preferably in the style of "." but I'm open to alternative suggestions for urls if need be...

like image 242
Yves Schelpe Avatar asked Jan 08 '14 14:01

Yves Schelpe


People also ask

What is Web API creating a Restfull Web API using ASP.NET MVC?

Select the Visual C# | Web project type from the project type tree view, then select the ASP.NET MVC 4 Web Application project type. Set the project's Name to ContactManager and the Solution name to Begin, then click OK. In the ASP.NET MVC 4 project type dialog, select the Web API project type. Click OK.

What is Web API in MVC with example?

Advertisements. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the . NET Framework.


2 Answers

Suffix the URL with a slash e.g. http://somedomain.com/api/people/staff.33311/ instead of http://somedomain.com/api/people/staff.33311.

like image 125
Danny Varod Avatar answered Sep 30 '22 06:09

Danny Varod


Following setting in your web.config file should fix your issue:

<configuration>     <system.webServer>         <modules runAllManagedModulesForAllRequests="true" /> 
like image 22
Kiran Avatar answered Sep 30 '22 06:09

Kiran