英文:
Declaring a route with one required parameter and multiple optional query strings
问题
我没有太多声明路由的经验,所以我想要的可能是无效的,但我考虑创建这样的路由:
givememoney/printdate/2023-05-23¤cy=USD&amonut=alot&forwhom=me,mom,dad
其中printdate
和其对应的值,如2023-05-23
,始终是必需的,而其余部分是可选的。
首先:这是有效的吗?
其次:如果有效,如何声明这样的路由?以下是我尝试过的方式,但是它是错误的:
[FunctionName("GiveMeMoney")]
public async Task<IActionResult> GiveMeMoney(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "givememoney/{printdate}/{optional?}")] HttpRequest req, ILogger log, string printdate, string? optional)
{
// .... stuff ...
}
希望这对你有帮助。
英文:
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&currency=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 ...
}
答案1
得分: 0
以下是您要翻译的内容:
第一部分:
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;
}
}
}
这里要注意,代码部分不会被翻译。
第二部分:
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:
这里也是代码和图片,没有需要翻译的文本内容。
英文:
>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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论