3

Hey I would like to achieve something that with dynamic parameters.

For example I have this Controller Action:

[HttpGet]
public async Task TestParam(string firtname, string lastname, string persnr)
{
    Console.WriteLine("Firstname: " + firtname);
    Console.WriteLine("Lastname: " + lastname);
    Console.WriteLine("Persnr: " + persnr);
}        

Is it possible that my request can look like this:

Url Firstname Lastname Persnr
URL/?firtname=Test&lastname=Test&persnr=123 Test Test 123
URL/?firtname=Test&persnr=123 Test 123
URL/?lastname=Test&persnr=123 Test 123
URL/?persnr=123 123

So basically that I don't have to write all parameters?

Because if I make them optional with

TestParam(string firtname = "", string lastname = "", string persnr = "")

I have to write the url like that

URL/?firtname=&lastname=&persnr=123

to only see the persnr.

What is the best way to achieve this?

5
  • You a model and put all in there and check each one and use what you need. Commented Nov 9, 2021 at 9:17
  • Which version of ASP.NET are you using? Commented Nov 9, 2021 at 9:47
  • Hey that was actually my fault, because it is not my programm. I though it was asp.net but it isn't it is a standard c# programm. If it were on asp.net I came up with this solution HttpUtility.ParseQueryString. Isn't it much better then having a model class? Commented Nov 9, 2021 at 9:57
  • I can read the parameter like that string firstname = HttpUtility.ParseQueryString(myUri.Query).Get("firstname"); Commented Nov 9, 2021 at 9:58
  • With Asp.net having a model class is better because of automatic model binding and validation and it works with all HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.) Commented Nov 9, 2021 at 10:13

2 Answers 2

2

You can create a model class for your query parameters like this:

public class TestQuery
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Persnr { get; set; }
}

Then you can replace your TestParam signature like this:

public async Task TestParam([FromUri]TestQuery queryParams)

For more information please check this MSDN article.

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

Comments

1

The Answer from Peter Csala worked but I'll have a second solution.

[HttpGet("{uri}")]
public async Task<string> TestParam(string uri)
{
    Uri myUri = new Uri("http://localhost:2000/?" + uri);

    string firstname = HttpUtility.ParseQueryString(myUri.Query).Get("firstname");
    string lastname = HttpUtility.ParseQueryString(myUri.Query).Get("lastname");
    string persnr = HttpUtility.ParseQueryString(myUri.Query).Get("persnr");

    Console.WriteLine("Firstname: " + firtname);
    Console.WriteLine("Lastname: " + lastname);
    Console.WriteLine("Persnr: " + persnr);
}

This will read all parameters your write in the uri. For example lastname is not set in the uri it will be empty.

HttpUtility.ParseQueryString

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.