针对如何在ASPWebApi中将多个键传递给数组?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展asp.net–AJAX将多个参数传递给WebApi、asp.net-core-webapi–
针对如何在ASP Web Api中将多个键传递给数组?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展asp.net – AJAX将多个参数传递给WebApi、asp.net-core-webapi – 在asp.net核心web api中上传多部分/表单数据文件、asp.net-mvc – 如何在ASP.NET MVC Web API中将URL作为参数传递给参数?、asp.net-mvc – 如何在ASP.NET MVC中将多个对象传递给ViewPage?等相关知识,希望可以帮助到你。
本文目录一览:- 如何在ASP Web Api中将多个键传递给数组?
- asp.net – AJAX将多个参数传递给WebApi
- asp.net-core-webapi – 在asp.net核心web api中上传多部分/表单数据文件
- asp.net-mvc – 如何在ASP.NET MVC Web API中将URL作为参数传递给参数?
- asp.net-mvc – 如何在ASP.NET MVC中将多个对象传递给ViewPage?
如何在ASP Web Api中将多个键传递给数组?
由于您的代码示例未显示API调用,因此可能很难提供答案。以我的经验,将您的InsertDTO模型转换为JSON字符串,然后将JSON字符串传递给Web API是正确的做法。
Can i use JSON.Stringify in code-behind of an ASP.Net project
var jsonStr = new JavaScriptSerializer().Serialize(myModel);
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-core-webapi – 在asp.net核心web api中上传多部分/表单数据文件
解决方法
使用.net核心,您可以利用新的IFormFile接口在同一帖子中上传图像和属性.例如:
[HttpPost("content/upload-image")] public async Task<IActionResult> UploadImage(MyFile upload)
MyFile类看起来像:
public class MyFile { public string userId { get; set; } public IFormFile File { get; set; } // Other properties }
您可以按如下方式访问属性和文件:
var file = upload.File // This is the IFormFile file var param = upload.userId // param
要将文件保留/保存到磁盘,您可以执行以下操作:
using (var stream = new FileStream(path,FileMode.Create)) { await file.File.copyToAsync(stream); }
.NET Framework
是的.根据您正在使用的客户端框架,您可以为内容类型 – 多部件配置Web API,然后执行以下操作:
[HttpPost] [Route("content/upload-image")] public async Task<HttpResponseMessage> Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // enter code here }
定义并设置保存图像的目录.
var root = HttpContext.Current.Server.MapPath("~/Content/Images/"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); }
设置StreamProvider并尝试获取模型数据,这是您提到的JSON.
var streamProvider = new MultipartFormDataStreamProvider(root); var result = await Request.Content.ReadAsMultipartAsync(streamProvider); if (result.FormData["model"] == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); }
现在访问请求中的文件.
try { // Deserialize model data to your own DTO var model = result.FormData["model"]; var formDto = JsonConvert .DeserializeObject<MyDto>(model,new IsoDateTimeConverter()); var files = result.FileData.ToList(); if (files != null) { foreach (var file in files) { // Do anything with the file(s) } } }
asp.net-mvc – 如何在ASP.NET MVC Web API中将URL作为参数传递给参数?
如下所示:
test.com/api/sales/getcustomerorders/test@test.com
我想将电子邮件地址作为参数传递给getcustomerorders操作.
我们可以传递使用查询字符串.但是我想像上面那样格式化URL.
谢谢.
解决方法
asp.net-mvc – 如何在ASP.NET MVC中将多个对象传递给ViewPage?
我想将几个(在这个例子中为2个)一些不同的数据传递给View.我最初的想法是简单地将各种对象包装成一个包含对象并沿着这种方式传递它们.然后从视图中,我会有类似的东西
var objContainer = ViewData.Model; var thisObject = objContainer.ThisObject; var thatObject = objContainer.ThatObject;
这些可以在母版页和查看页中单独使用.
这是“最好的”方式吗?
解决方法
namespace Core.Presentation { public class SearchPresentation { public IList<StateProvince> StateProvinces { get; set; } public IList<Country> Countries { get; set; } public IList<Gender> Genders { get; set; } public IList<AgeRange> AgeRanges { get; set; } } }
然后我确保我的View是一个强类型视图,它使用该表示类的泛型版本:
public partial class Search : ViewPage<SearchPresentation>
在View中,我可以使用Intellisense轻松浏览项目.
关于如何在ASP Web Api中将多个键传递给数组?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于asp.net – AJAX将多个参数传递给WebApi、asp.net-core-webapi – 在asp.net核心web api中上传多部分/表单数据文件、asp.net-mvc – 如何在ASP.NET MVC Web API中将URL作为参数传递给参数?、asp.net-mvc – 如何在ASP.NET MVC中将多个对象传递给ViewPage?等相关内容,可以在本站寻找。
本文标签: