声明一个带有一个必需参数和多个可选查询字符串的路由。

huangapple go评论75阅读模式
英文:

Declaring a route with one required parameter and multiple optional query strings

问题

我没有太多声明路由的经验,所以我想要的可能是无效的,但我考虑创建这样的路由:

givememoney/printdate/2023-05-23&currency=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&amp;currency=USD&amp;amonut=alot&amp;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(&quot;GiveMeMoney&quot;)]
public async Task&lt;IActionResult&gt; GiveMeMoney(
[HttpTrigger(AuthorizationLevel.Function, &quot;get&quot;, Route = &quot;givememoney/{printdate}/{optional?}&quot;)] 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&lt;Function1&gt;();
        }

        [Function(&quot;Function1&quot;)]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, &quot;get&quot;, Route = &quot;emo/{date}&quot;)] HttpRequestData req, FunctionContext context,
            string date, string? Sur)
        {
            var logger = context.GetLogger(&quot;Function1&quot;);

            var q = req.Url.Query;
            var qp = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(q);
            string name = qp[&quot;name&quot;];            
            if (!string.IsNullOrEmpty(Sur))
            {
                logger.LogInformation($&quot;SurName: {Sur}&quot;);
            }

            logger.LogInformation($&quot;name:{name}&quot;);
            

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.WriteString(&quot;Completed Rithwik&quot;);

            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&amp;Sur=Bojja

声明一个带有一个必需参数和多个可选查询字符串的路由。

Output:

声明一个带有一个必需参数和多个可选查询字符串的路由。

huangapple
  • 本文由 发表于 2023年5月24日 21:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定