I don't have much experience in declaring routes so what I want might even be invalid but I am thinking of having a route like this:
givememoney/printdate/2023-05-23¤cy=USD&amonut=alot&forwhom=me,mom,dad
where printdate
and some value for it like 2023-05-23
is always required.
but the rest of it is optional.
First: Is this valid?
Second: If it is valid, how can I declare such a route? Here is what I tried but it is wrong.
[FunctionName("GiveMeMoney")]
public async Task<IActionResult> GiveMeMoney(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "givememoney/{printdate}/{optional?}")] HttpRequest req, ILogger log, string printdate, string? optional)
{
// .... stuff ...
}
First: Is this valid?
Yes, this is Valid and below is my code:
Function1.cs:
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace FunctionApp24
{
public class Function1
{
private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function1>();
}
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "emo/{date}")] HttpRequestData req, FunctionContext context,
string date, string? Sur)
{
var logger = context.GetLogger("Function1");
var q = req.Url.Query;
var qp = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(q);
string name = qp["name"];
if (!string.IsNullOrEmpty(Sur))
{
logger.LogInformation($"SurName: {Sur}");
}
logger.LogInformation($"name:{name}");
var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString("Completed Rithwik");
return response;
}
}
}
Here name is required parameter and Sur is Optional Parameter.
Second: If it is valid, how can I declare such a route?
Firstly, to call only required then use below:
http://localhost:7115/api/emo/2023-05-25?name=Rithwik
Output:
Optional Calling :
http://localhost:7115/api/emo/2023-05-25?name=Rithwik&Sur=Bojja
Output: