Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to make this Web Api to work?
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Miscellaneous
Thread ID:
01658678
Message ID:
01658828
Views:
57
Take a look and see what gets sent over the wire with both the manual request and the encoded request and compare...

Whatever you type in the URL and gets fixed up and works is what you have to match.In your example for the one that works you're not sending the / that is encoded.

> http://siriuswarecontrol.web/SiriuswareControl/api/matrixTemplates/getAttributeValues/1/Black%2FWhite

This is is a valid URL that has `Black/White` as value. If that doesn't work there's a problem with the HTTP client (which would be Angular's http.get() in this case).

I still believe that standard XHR will fix up URL Path segments properly automatically if you just provide the raw string. But if `/` is in that path that's the one character that won't work for auto fix up because it IS a path separator. So that one has to be encoded.

Again none of would be a problem if you properly use POST/PUT for data rather than the URL. In general you should use URL and GET requests for IDENTITIFIERS only (ie. Ids) not for DATA. If you have spaces and extended characters in your GET paths/values then you probably are using Data as identifiers which is rarely a good idea and it's better to post those values to the server.

+++ Rick ---

>>> services.Http.get("api/matrixTemplates/getAttributeValues/" + id + "/" + criteria);
>>>
>>>My question to you is - how exactly I need to code the above so it would work for any character criteria?
>>
>>You shouldn't have to encode anything because the XHR request (ie. the Webrequest internal tot the browser) will automatically fix up the URL. If you want to be extra safe and don't rely on that behavior you can UrlEncode the parameterized value.
>>
>>Either way passing a parameters object in the .get() call will never fill these parameters - you need to hard code those as you show above or by using `encodeURIComponent()` to encode the values.
>>
>>
>>services.Http.get("api/matrixTemplates/getAttributeValues/" + encodeURIComponent(id) + "/" + encodeURIComponent(criteria));
>>
>>
>>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
>
>I can not make it to work. I'm getting not found error when I try to send
>
>http://siriuswarecontrol.web/SiriuswareControl/api/matrixTemplates/getAttributeValues/1/Black%2FWhite
>
>The signature of my method now is
>
>
> [Route("getAttributeValues/{attributeId}/{criteria}")]
>        [HttpGet]
>        public IHttpActionResult GetAttributeValues(int attributeId, string criteria="")
>
>If I send
>
>http://siriuswarecontrol.web/SiriuswareControl/api/matrixTemplates/getAttributeValues/1/Black
>
>it works fine.
>
>The call from the service is now:
>
>
>MatrixTemplatesService.prototype.getAttributeValues = function (attributeId, criteria) {
>        var deferred = services.Deferred;
>
>        // let keyValuePair = { Key: attributeId, Value: criteria };
>
>        let url = "api/matrixTemplates/getAttributeValues/" + attributeId;
>
>        if (criteria) {
>            url = url + "/" + encodeURIComponent(criteria);
>        }
>
>        services.Http.get(url)
>
>I think I'm going to revert now to the working solution.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform