想了解将看起来像日期的DateTime或String类型作为参数传递给URL/(.netwebapi)的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于.net–调用WebMethod传
想了解将看起来像日期的 DateTime 或 String 类型作为参数传递给 URL / (.net webapi)的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于.net – 调用WebMethod传递Dictionary作为参数、Angular 8与.NetCore WebAPI传递对象作为参数、asp.net – AJAX将多个参数传递给WebApi、asp.net – 如何使用Web API属性路由传递DateTime参数?的新知识。
本文目录一览:- 将看起来像日期的 DateTime 或 String 类型作为参数传递给 URL / (.net webapi)
- .net – 调用WebMethod传递Dictionary作为参数
- Angular 8与.NetCore WebAPI传递对象作为参数
- asp.net – AJAX将多个参数传递给WebApi
- asp.net – 如何使用Web API属性路由传递DateTime参数?
将看起来像日期的 DateTime 或 String 类型作为参数传递给 URL / (.net webapi)
首先,整个方法在我看来是不正确的,似乎使用实体框架可以解决您的类型问题。 回答您的问题:
- 您只能在
string
中使用url
。 - 如果我理解正确的话,您只需要一种方法即可从
string
类中获取Item
,如下所示
public class Item {
Date date { get; set; }
public dateToString()
{
return $"{this.date.Day}-{this.date.Month}-{this.date.Year}";
}
}
- 每次需要将
dateToString
作为date
传递时只需使用string
,这样您就不会丢失格式。
.net – 调用WebMethod传递Dictionary作为参数
[WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static override ResultObject<List<PatientInfo>> GetResults(Dictionary<string,string> query) { ResultObject<List<PatientInfo>> resultObject = null; if (!query.ContainsKey("finValue")) { resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter from the query"); } string finValue = query["finValue"]; if(finValue == null) { resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter value from the query"); } var patientData = GetPatientsByFin(finValue); resultObject = new ResultObject<List<PatientInfo>>(patientData); return resultObject; } }
我的问题是:如何传递和反序列化Dictionary参数?
解决方法
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolBoxItem(false)] [ScriptService] public class TestService : System.Web.Services.WebService { [WebMethod] public String PostBack(Dictionary<string,string> values) { //You should have your values Now... return "Got it!"; } }
然后,当你想要调用它时,你可以传递这样的东西.不确定你是否使用jQuery,但这是使用jQuery的ajax方法的一个例子.
var valueObject = {}; valueObject['key1'] = "value1"; valueObject['secondKey'] = "secondValue"; valueObject['keyThree'] = "3rdValue"; $.ajax({ url: 'TestService.asmx/PostBack',type: 'POST',dataType: 'json',contentType: 'application/json; charset=utf-8',data: JSON.stringify({ values: valueObject }),success: function (data) { alert(data); },error: function (jqXHR) { console.log(jqXHR); } });
Angular 8与.NetCore WebAPI传递对象作为参数
您是否尝试将userDetails中的 any 类型替换为字符串:
RegisterUser(userDetails: string ):Observable<UserInfo> {
return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser/',userDetails,this.httpOptions)
}```
,
注意:我没有对其进行测试,但这可以帮助您解决问题。
@SAI,您好,如果您要访问的主机/端口是正确的,我将尝试进行以下更改:
在网址末尾删除多余的“ /”,并删除JSON.stringify调用
RegisterUser(userDetails: string ):Observable<UserInfo> {
return this.http.post<UserInfo>('https://localhost:44322/api/User/RegisterUser',this.httpOptions)
}
更新控制器的路由配置(也请检查我添加到操作中的注释)
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
SqlRepository repository = new SqlRepository();
[HttpPost]
public int RegisterUser(UserInfo userInfo)
{
// some details here. I am guessing this is an async operation
// In which case I think you need to change this code also to wait
// for the response and the return type of this action to Task<int>
return repository.RegisterUsers(userInfo);
}
}
asp.net – AJAX将多个参数传递给WebApi
$.ajax({ url: url,dataType: 'json',type: 'Post',data: {token:"4",Feed:{"id":0,"message":"Hello World","userId":4} } });
服务器端Web API:
[HttpPost] public HttpResponseMessage Post(string token,Feed Feed) { /* Some code */ return new HttpResponseMessage(HttpStatusCode.Created); }
Error Code 404: {“message”:”No HTTP resource was found that matches
the request URI ‘localhost:8080/api/Feed’.”,”messageDetail”:”No action
was found on the controller ‘Feed’ that matches the request.”}
为什么我收到此错误以及为什么我无法将多个参数POST到我的API?
解决方法
public class Myviewmodel { public string Token { get; set; } public Feed Feed { get; set; } }
您的控制器操作将作为参数:
[HttpPost] public HttpResponseMessage Post(Myviewmodel model) { /* Some code */ return new HttpResponseMessage(HttpStatusCode.Created); }
最后调整你的jQuery调用将其作为JSON发送:
$.ajax({ url: url,type: 'POST',contentType: 'application/json',data: JSON.stringify({ token: '4',Feed: { id: 0,message: 'Hello World',userId: 4 } }) });
AJAX调用需要注意的重要事项:
>将请求contentType设置为application / json>将数据包装在JSON.stringify函数中,以有效地将javascript对象转换为JSON字符串>删除无用的dataType:’json’参数. jQuery将自动使用服务器发送的Content-Type响应头来推断如何解析传递给成功回调的结果.
asp.net – 如何使用Web API属性路由传递DateTime参数?
当我尝试将日期整合到游戏中时会变坏.
这是路线:
[Route("{id:Guid}/{from:DateTime}/{to:DateTime}")]
当我这样做时,我可以完美地传递类似的东西
.../[id]/2012-01-01/2013-01-01
然而,当我到达需要时间信息的地步时,它变得非常糟糕,让我们想象一下:
.../[id]/2012-01-01/2013-01-01 1:45:30 PM/2013-01-01 1:45:30 PM
似乎空间正常,但“:”正在阻塞.所以我虽然应该使用我自己的格式,因为yyyyMMddhhmm.这给出了以下URL:
.../[id]/201301031147/201401031147
但是,.NET并不期望这是一个DateTime,也不知道如何使用它.所以我使用IHttpRouteConstraint来允许它.
问题是,在我告诉它没关系后,它仍然不知道如何处理它…
所以我的问题是,你如何将DateTime传递给路线?
解决方法
从路由规范中删除它,但将它们作为参数保留在方法中,模型绑定器将绑定它们.或者,您也可以将它们作为参数删除,并通过Context查询字符串属性或ControllerContext.Request.GetQueryNameValuePairs()访问它们.
你会有吗?fromDate = 2012-01-01T1:45:30 PM& toDate = 2013-01-01T1:45:30PM
今天关于将看起来像日期的 DateTime 或 String 类型作为参数传递给 URL / (.net webapi)的讲解已经结束,谢谢您的阅读,如果想了解更多关于.net – 调用WebMethod传递Dictionary作为参数、Angular 8与.NetCore WebAPI传递对象作为参数、asp.net – AJAX将多个参数传递给WebApi、asp.net – 如何使用Web API属性路由传递DateTime参数?的相关知识,请在本站搜索。
本文标签: