4

Net core application. I have one GET api as below:

        [HttpGet]
        [Route("configId={configId}&quoteId={quoteId}"), EnableQuery()]
        public async Task<IEnumerable<Scenario>> GetScenario(string configId, int quoteId)
        {
            var result = await configScenarioService.GetScenarioAsync(configId, quoteId);
            if (result.IsSuccess)
            {
                return result.scenarioResults;
            }
            return new List<Scenario>();
        }

I am trying to hit from Postman as below:

https://localhost:44362/api/v1/Scenario/configId=JBEL+ASS_60_SG_5.2-145_MY21_T102.5_25y&quoteId=236

Unfortunately, this is giving 404 error. Maybe the '+' sign is causing the issue. After looking into some documentation, I tried as below:

1. https://localhost:44362/api/v1/Scenario/configId="+ encodeURIComponent(BEL+ASS_60_SG_5.2-145_MY21_T102.5_25y) +"&quoteId=236

This didn't work for me and still gave a 404 error.

How can this be fixed?

4 Answers 4

2

since you have + sign you have to encode your url, for + url encoded is %2B https://www.w3schools.com/tags/ref_urlencode.asp

..../Scenario?configId=JBEL%2BASS_60_SG_5.2-145_MY21_T102.5_25y&quoteId=236

and since you have 404 you have to fix an action route too

[Route("~/api/v1/Scenario")]
 public async Task<IEnumerable<Scenario>> GetScenario([FromQuery] string configId, [FromQuery] int quoteId)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this is working from swagger but my JS code files. I have below code const urlEncode = encodeURIComponent(configurationId) doGet(Scenario/GetScenario/configId=${urlEncode}&quoteId=${quote} when I see in network tab it is getting converted as configId=JBEL%2BASS_60_SG_5.2-145_MY21_T102.5_25y but when i try from swagger it is getting converted as configId=JBEL%252BASS_60_SG_5.2-145_MY21_T102.5_25y So difference in %2 and %252
@MrPerfect It should be %2B, see my updated answer. You can just try this string.replace("+", "%2B") if you have problems with encoding
2

Then try this:

[ApiController]
[Route("api/v1/[controller]/[action]")]
public class YourController : ControllerBase
{
    //... ctor and your other stuff

    [HttpGet("{configId}/{quoteId}", Name = "Scenario")]
    [ProducesResponseType(typeof(IEnumerable<Scenario>)]
    public async Task<ActionResult<IEnumerable<Scenario>>> GetScenario(string configId, string quoteId)
    {
        
    }
}

Comments

1

Try this

[HttpGet]
        [Route, EnableQuery()]
        public async Task<IEnumerable<Scenario>> GetScenario([FromQuery(Name = "configId")]string configId, [FromQuery(Name = "quoteId")]int quoteId)
        {

2 Comments

Thanks for your answer. I added above code [HttpGet] [Route("GetScenario"), EnableQuery()] public async Task<IEnumerable<Scenario>> GetScenario([FromQuery(Name = "configId")] string configId, [FromQuery(Name = "quoteId")] int quoteId) but dint work for me
from javascript also i tried but dint work out for me const urlEncode = encodeURIComponent(configurationId) doGet(Scenario/configId=${urlEncode}&quoteId=${quote}
-1

what worked is const urlEncode = encodeURIComponent(encodeURIComponent(configId)) but still i am unable to figure it why i should use encodeURIComponent(encodeURIComponent) in my js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.