Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

http get - Cannot call HttpGet method with parameters in C# .Net Framework

I have a problem with calling a HttpGet method with parameters. I am working in .Net Framework 4.7.2. I am using Advanced REST Client to test my methods. When I call a method with no parameters, it works just fine, but when I want to call a method with parameters I am not able to. Where is the mistake I am making? Here are my 2 methods.

[HttpGet]
    [Route("action1")]
    public IHttpActionResult action1()
    {
        return Ok("blabla");
    }

    [HttpGet]
    [Route("action2/{parameter}")]
    public IHttpActionResult action2(string parameter)
    {
        return Ok(parameter);
    }

And here is how I call the second method. As I said, the first one works just fine. Advanced Rest client usage

Here is the message I keep getting:{ "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44338/api/login/action2/?parameter="blabla"'.", "MessageDetail": "No action was found on the controller 'Login' that matches the request." }

question from:https://stackoverflow.com/questions/65935628/cannot-call-httpget-method-with-parameters-in-c-sharp-net-framework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The action is using a route parameter which magically matches parts of the url string (not query parameters) to the actions parameters.

You should either change the URL in Postman to https://localhost:44338/api/login/action2/blabla

or you could remove {parameter} from the Route Attribute


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...