2

I'm trying to use the web api in c# so far its been working pretty well but i just have a route that doesn't want to work and i searched a bit around for people with similar problems but their solution didn't seem to work. I'm trying to have a specific route and capturing an email address in the string, here is my Controller:

[Authorize] [RoutePrefix("api/contact")] public class ContactController : ApiController { [Route("list/{id:int}")] [HttpGet] public ContactList GetList(int id) { BasicAuthenticationIdentity identity = (BasicAuthenticationIdentity)this.User.Identity; ContactModel contactModel = new ContactModel(identity.accountId); return (contactModel.GetList(id)); } [Route("list/{id:int}")] [HttpPost] public void PostList(int id) { BasicAuthenticationIdentity identity = (BasicAuthenticationIdentity)this.User.Identity; // To be implemented } [Route("attribute/{contactKey}")] [HttpGet] public IEnumerable<ContactData> GetContactAttributeKey(string contactKey) { BasicAuthenticationIdentity identity = (BasicAuthenticationIdentity)this.User.Identity; ContactModel contactModel = new ContactModel(identity.accountId); return (contactModel.GetContactAttribute(contactKey)); } } 

The list route works well but when i try something like http://localhost/api/contact/attribute/[email protected] i keep getting HTTP Error 404.0 - Not Found because it seems it can't find the route. Is there something wrong in this?

I also have this in the webconfig

// Itinéraires de l'API Web config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

I tried to see if other routes could be set that are the same but there is no other, i just have an account and segment route other than that, if anyone got some suggestion i'm open to everything right now :/

1
  • What happens if you pass a string value without '@'? Commented Nov 17, 2015 at 15:20

2 Answers 2

3

You should use %40 instead of @ symbol and %2E instead of ..

http://localhost/api/contact/attribute/test%40test%2Ecom

This is called URL encoding, some characters are not valid in URL.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that before but i just did it again by removing the '.' of the test.com making it like test%40testcom and now it works but with the test%40test.com it doesn't work. I'll try to check why but afaik this isn't suppose to be encoded as a character right?
1

Okay its a comibnation of the answer of dotctor (using the url encode) and also there is a need to enable this in your web.config

<configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.