gpt4 book ai didi

asp.net-core - 使用asp .net core 查询参数路由

转载 作者:行者123 更新时间:2023-12-03 11:49:41 25 4
gpt4 key购买 nike

我想使用 url 查询参数而不是使用 .net 核心 API 的路径参数。

Controller

[Route("api/[controller]/[action]")]
public class TranslateController : Controller
{
[HttpGet("{languageCode}")]
public IActionResult GetAllTranslations(string languageCode)
{
return languageCode;
}
}

startup.cs 仅使用默认设置
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonOptions.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

services.AddLogging();
services.AddSingleton<IConfiguration>(Configuration);

services.AddSwaggerGen(c =>
{
c.SingleApiVersion(new Info
{
Version = "v1",
Title = "Translate API",
Description = "bla bla bla description",
TermsOfService = "bla bla bla terms of service"
});
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseMvc();

app.UseSwagger();
app.UseSwaggerUi();
}

我的招摇请求看起来像这样
enter image description here

我的 postman 请求在这里
enter image description here

我想更改我的 GetAllTranslations 以接受查询参数而不是路径参数,但是当我将 postman 查询更改为
http://localhost:42677/api/Translate/GetAllTranslations?languageCode=en

我会收到错误 404 Not found 所以很明显我的 Controller 路径设置不正确,但我不知道如何做到这一点......有什么想法吗?

我曾尝试删除 [HttpGet("{languageCode}")] 属性,但我一直收到空参数而不是值。

最佳答案

这就是你要找的

public IActionResult GetAllTranslations([FromQuery]string languageCode)

关于asp.net-core - 使用asp .net core 查询参数路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41022983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com