是否有通过查询字符串传递数组的标准方法?
为了清楚起见,我有一个包含多个值的查询字符串,其中一个值是数组值.我希望将查询字符串值视为数组 - 我不希望数组被展开,以便它与其他查询字符串变量无法区分.
此外,根据这篇帖子的答案,作者建议不定义对数组的查询字符串支持.这准确吗?
编辑:
基于@Alex的回答,没有标准的方法可以做到这一点,所以我的后续工作是什么是一种简单的方法来识别我正在阅读的参数是PHP和Javascript中的数组?
将多个参数命名为同名是否可以接受,这样我知道它们属于一个数组?例:
?myarray=value1&myarray=value2&myarray=value3...
Run Code Online (Sandbox Code Playgroud)
或者这是不好的做法?
我正在尝试将通过AJAX的int(或IEnumerable)数组传递给MVC操作,我需要一些帮助.
javascript是
$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...
Run Code Online (Sandbox Code Playgroud)
并且控制器动作是
public ActionResult MyAction(IEnumerable<int> arrayOfValues )
Run Code Online (Sandbox Code Playgroud)
目前请求的格式为
controller/MyAction?_=1301503418429&arrayOfValues[]=491&arrayOfValues[]=368&arrayOfValues[]=235&arrayOfValues[]=437
Run Code Online (Sandbox Code Playgroud)
所以我几乎就在那里,如果我取下方括号,我会得到正确的答案.我应该如何将该数组传递给我的get,以便控制器可以识别它是什么?
非常感谢您的帮助
戴夫
我想做这个,但我也想能够在阵列传递到查询字符串.我尝试过这样的事情:
http://www.sitename.com/route?arr[]=this&arr[]=that
http://www.sitename.com/route?arr[]=this&that
http://www.sitename.com/route?arr[0]=this&arr[1]=that
http://www.sitename.com/route?arr0=this&arr1=that
http://www.sitename.com/route?arr=this&arr=that
Run Code Online (Sandbox Code Playgroud)
我在C#代码中的路由如下所示:
[Route("route")]
[HttpGet]
public void DoSomething(string[] values)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
但在所有这些情况下,当它到达C#代码时,值始终为null.我需要什么查询字符串来传递字符串数组?
我的api客户端代码在查询字符串中发送身份验证令牌,如:
www.example.com/api/user/get/123?auth_token=ABC123
Run Code Online (Sandbox Code Playgroud)
我正在使用Mvc Web api控制器,我有一个过滤器来检查auth_token是否有效,但我不知道如何访问请求查询字符串值.
这就是我现在正在做的事情,但显然是错误的:
以下代码段位于我的过滤器内部,该过滤器继承自:
ActionFilterAttribute
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
if (actionContext.Request.Properties.ContainsKey("auth_token") &&
actionContext.Request.Properties["auth_token"].ToString() == "ABC123")
{
...
}
}
Run Code Online (Sandbox Code Playgroud) 你如何建模在ASP.NET Core 1 Web API中使用GET从URI中绑定数组(隐式或显式)?
在ASP.NET Web API pre Core 1中,这有效:
[HttpGet]
public void Method([FromUri] IEnumerable<int> ints) { ... }
Run Code Online (Sandbox Code Playgroud)
你是如何在ASP.NET Web API Core 1(又名ASP.NET 5又名ASP.NET vNext)中做到这一点的?文档什么都没有.
我试图将整数数组发送到我的操作方法,代码看起来像这样:
[HttpGet]
public async Task<IActionResult> ServicesByCategoryIds([FromQuery] int[] ids)
{
var services = await _accountsUow.GetServiceProfilesByCategoryIdsAsync(ids);
return Ok(services);
}
Run Code Online (Sandbox Code Playgroud)
我这样调用该方法:https:// localhost:44343 / api / accounts / servicesbycategoryids?ids = 1&ids = 2
但是,即使我在查询字符串中传递ID时,调用此方法时总会得到一个空数组。我正在使用.net core 2.1。
我搜索过的所有内容都表明这实际上是完成的方式。。。我在这里缺少什么吗?
谢谢!
如果我向删除端点发送一个 id,我的 webAPI 解决方案目前可以工作:
DELETE /api/object/1
Run Code Online (Sandbox Code Playgroud)
和:
[HttpDelete]
public HttpResponseMessage DeleteFolder(int id)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端应用程序中,我有一个允许多次删除的 UI - 现在它只是在循环中为每个选定的 id 调用此端点,这不是超级高性能。在这种情况下,我希望能够将一组 Ids 发送到 Delete 方法......如何实现?
我有以下内容:
[HttpDelete]
public HttpResponseMessage DeleteFolder(int[] ids)
{
Run Code Online (Sandbox Code Playgroud)
而我正试图用这个:
DELETE http://localhost:24144/api/folder/1483;
DELETE http://localhost:24144/api/folder/[1483]
Run Code Online (Sandbox Code Playgroud)
但是这些在delete方法中都是null - 如何在不将数据放入正文的情况下发送数据(因为这是DELETE请求)?
我的路由有这个:
routes.MapHttpRoute(
name: "Folder",
routeTemplate: "api/folder/{id}",
defaults: new { controller = "Folder", id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud) 我是asp.net mvc webapi的新手.我正在创建一个webapi服务.在这个服务中,我将参数作为数组类发送.
以下是我的服务:
[AcceptVerbs("GET", "POST")]
public HttpResponseMessage addBusOrder(string UserUniqueID, int PlatFormID,
string DeviceID, int RouteScheduleId,
string JourneyDate, int FromCityid,
int ToCityid, int TyPickUpID,
Contactinfo Contactinfo, passenger[] pass)
{
//done some work here
}
public class Contactinfo
{
public string Name { get; set; }
public string Email { get; set; }
public string Phoneno { get; set; }
public string mobile { get; set; }
}
public class passenger
{
public string passengerName { get; set; }
public string …Run Code Online (Sandbox Code Playgroud) 假设我有这个URL:
api.XXX.com/cities?risk=a,b&rating=x,y
Run Code Online (Sandbox Code Playgroud)
我想要的是风险类别为A或B且评级为X或Y的城市列表.我应该如何实施?
我知道我可以获得风险和评级键的值,但接着是什么?是否最好简单地解析字符串并使用switch语句?还是有更好的方法?
我有这个Web API方法:
[Route("api/[controller]")]
[ApiController]
public class SubjectsController : ControllerBase
{
[HttpGet("children")]
public IActionResult GetAllFromChildren([FromQuery]int[] childrenIds)
{
// omitted for brevity
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过传递查询字符串的Ajax来调用它,但似乎无法正常工作。我的Ajax呼叫看起来像这样:
$.ajax({
url: "/api/subjects/children?childrenIds=1&childrenIds=2&childrenIds=3",
method: "GET",
contentType: "application/json; charset=utf-8"
})
Run Code Online (Sandbox Code Playgroud)
该方法被调用,但是不会填充int数组。我究竟做错了什么?
所以我期望的是当 Angular 6 应用程序使用 GET 方法请求时,将唯一标识符的列表或数组作为参数发送到 ASP.NET Core Web API,然后 ASP.NET Core 将带来仅与数组相关的信息返回到 angular 应用程序的唯一标识符。
我的 Web API 代码看起来像这样。
[HttpGet]
public ActionResult GetByGuids(List<Guid> Guids)
{
// some code here...
return Ok(_someService.someAction(Guids));
//Guids will be passed as a parameter to the function of service layer
}
Run Code Online (Sandbox Code Playgroud)
根据我自己的研究,我无法将唯一标识符数组添加到正文中,因为这是 GET 方法。
我必须添加 [FromQuery] 因为如果我在参数是数组或列表时不添加它会产生错误。
如果 [FromQuery] 是处理这种情况的唯一方法,那么我不知道应该如何为 Angular 编写代码。
请帮忙。
先感谢您。
c# ×7
asp.net-core ×4
asp.net-mvc ×3
query-string ×2
.net-4.5 ×1
.net-core ×1
ajax ×1
angular ×1
arrays ×1
asp.net5 ×1
jquery ×1
json ×1
php ×1
rest ×1
typescript ×1