如何在MVC中将数据从MVC发布到webapi post方法

编程入门 行业动态 更新时间:2024-10-23 04:49:54
本文介绍了如何在MVC中将数据从MVC发布到webapi post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建应用程序原型,我在尝试从C#MVC Controller发送请求标头和正文中的数据,并创建了web api项目Post动作来处理请求。 我的代码是这样的:: MVC项目代码发布请求:

public class HomeController:Controller { public async Task< ActionResult> Index() { VM VM = new VM(); VM.Name =TEST Name; VM.Address =TEST地址; using(var client = new HttpClient()) { client.BaseAddress = new Uri(http:// localhost:58297); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(application / json)); client.DefaultRequestHeaders.Add(username,test); var json = JsonConvert.SerializeObject(VM); var content = new StringContent(json,Encoding.UTF8,application / json); var result1 = await client.PostAsync(api / Values / Post,content); } return View(); } }

我在WEB API项目中的代码:

// POST API /值公共IHttpActionResult邮政([FromBody] API.VM VM) {尝试 { HttpRequestMessage重新=新HttpRequestMessage(); StreamWriter sw = new StreamWriter(@E:\ Applele \ txt.log,false); var headers = re.Headers; string token =; if(headers.Contains(username)) { token = headers.GetValues(username)。First(); } sw.WriteLine(From header+ token); sw.WriteLine(From BODY+ vm.Name); sw.WriteLine(From BODY+ vm.Address); sw.WriteLine(Line2); sw.Close(); 返回Ok(成功); } catch(exception ex) {返回InternalServerError(ex); } }

我所理解的是[FromBody] API.VM vm从Http请求体获取数据,这意味着vm对象从HTTP Request body获取数据。我能够获取请求体。我无法理解如何从MVC控制器传递数据(我想传递JSON数据)并在WEB Api post方法中检索数据? 我尝试了什么: 我使用过

client.DefaultRequestHeaders.Add(username,test) ;

在MVC项目中传递头数据和

HttpRequestMessage re = new HttpRequestMessage(); var headers = re.Headers; string token =; if(headers.Contains(username)) { token = headers.GetValues(username)。First(); }

用于获取数据的WEB API项目中的但是我无法获得用户名值,解决方案 无需构建具有 新的对象的

HttpRequestMessage重新= 新 HttpRequestMessage();

使用静态请求而不是

Request.Headers.TryGetValue( username, out var test);

I am creating prototype of application where in I am trying to send data in request header and body from C# MVC Controller and also created web api project Post action to process the request. My code goes like this:: MVC Project code to Post Request:

public class HomeController : Controller { public async Task<ActionResult> Index() { VM VM = new VM(); VM.Name = " TEST Name"; VM.Address = " TEST Address "; using (var client = new HttpClient()) { client.BaseAddress = new Uri("localhost:58297"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("username","test"); var json = JsonConvert.SerializeObject(VM); var content = new StringContent(json, Encoding.UTF8, "application/json"); var result1 = await client.PostAsync("api/Values/Post", content); } return View(); } }

My code in WEB API project :

// POST api/values public IHttpActionResult Post([FromBody]API.VM vm) { try { HttpRequestMessage re = new HttpRequestMessage(); StreamWriter sw = new StreamWriter(@"E:\Apple\txt.log", false); var headers = re.Headers; string token = ""; if (headers.Contains("username")) { token = headers.GetValues("username").First(); } sw.WriteLine("From header" + token); sw.WriteLine("From BODY" + vm.Name); sw.WriteLine("From BODY" + vm.Address); sw.WriteLine("Line2"); sw.Close(); return Ok("Success"); } catch (Exception ex) { return InternalServerError(ex); } }

What I have understood is [FromBody]API.VM vm gets data from Http request body which means vm object is getting data from HTTP Request body.I am able to get request body. I am not able to understand how do I pass data in header from MVC controller (I want to pass JSON Data) and retrieve data in WEB Api post method? What I have tried: I have used

client.DefaultRequestHeaders.Add("username","test");

in MVC project to pass header data and

HttpRequestMessage re = new HttpRequestMessage(); var headers = re.Headers; string token = ""; if (headers.Contains("username")) { token = headers.GetValues("username").First(); }

in WEB API project to get data but I am not able to get username value,

解决方案

No need to construct a new object with

HttpRequestMessage re = new HttpRequestMessage();

use static Request instead

Request.Headers.TryGetValue("username", out var test);

更多推荐

如何在MVC中将数据从MVC发布到webapi post方法

本文发布于:2023-10-11 23:20:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   方法   数据   如何在   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!