1

What I am trying is to pass a parameter to the controller from typescript. But it's not passing to method.

below is my typescript code,

multiCountryConfig() {

var multiCountry = "Test";

this.http.get('/Home/MultiCountryConfiguration', {
  params: {
    multiCountry: multiCountry,
  }
}).subscribe(result => {
  console.log(result)
}, error => {
  console.log(error)
});

}

below one is my homecontroller method code,

    [HttpGet]
    public IActionResult MultiCountryConfiguration(string multiCountry)
    {
        var key = "test";
        var value = "en-gb";
        CookieOptions options = new();
        options.Expires = DateTime.Now.AddHours(1);
        HttpContext.Response.Cookies.Append(key, value, options);

        return Ok("created");
    }

I don't know whether it's correct. Please someone guide me on this. Thank you.

2
  • Did you tried to debug it in your browser Network tab? (check client request) Commented Jul 7, 2021 at 8:05
  • And send test request from Postman to your API and compare requests from Angular app. Commented Jul 7, 2021 at 8:07

3 Answers 3

1

What I am trying is to pass a parameter to the controller from typescript. But it's not passing to method.

Getting the same 404 error. GET https://localhost:44335/Home/MultiCountryConfiguration?multiCountry=Test 404

To troubleshoot the issue, please try to modify the action method like below.

[HttpGet("/Home/MultiCountryConfiguration")]
public IActionResult MultiCountryConfiguration([FromQuery]string multiCountry)
{
    //...

Besides, please make sure your backend site does host on https://localhost:44335, not on another port or http.

Note: your Angular frontend code seems ok, it could help make http request(s) and pass data through querystring. If the routing for your backend is ok, it should work as expected.

Request from frontend

enter image description here

Backend action method

enter image description here

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

Comments

0

When you use get method, you can send params via multiple ways.

  1. simple way is adding the params in the URL like this:
multiCountryConfig() {

var multiCountry = "Test";
const URL = `/Home/MultiCountryConfiguration?multiCountry=${multiCountry}`
this.http.get(URL).subscribe(result => {
  console.log(result)
}, error => {
  console.log(error)
});

}
  1. Another way of doing this can be using Params from HTTPModule
multiCountryConfig() {

 let params = new HttpParams();
    params = params.append('_page', 1);
    params = params.append('_limit', 10); 

const multiCountry = "Test";

this.http.get("/Home/MultiCountryConfiguration", {params: params}).subscribe(result => {
  console.log(result)
}, error => {
  console.log(error)
});

}
  

But It seems the path you are using in the URL seems incomplete, you might need to add the domain to work it properly and request the resource. Your .net services must be running on certain port and ip address ex: http://localhost:8080/Home/MultiCountryConfiguration. Please check where you web service is running and add the proper path.

2 Comments

Getting the same 404 error. GET localhost:44335/Home/… 404
Try using postman, the error says it doesn't find the route you mentioned. Please register it corrrectly.
0
 [HttpGet]   
public IActionResult MultiCountryConfiguration([FromBody]string multiCountry)
{
    var key = "test";
    var value = "en-gb";
    CookieOptions options = new();
    options.Expires = DateTime.Now.AddHours(1);
    HttpContext.Response.Cookies.Append(key, value, options);
    return Ok("created");
}
  • Edit #1: fix code section

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.