标签:WebApi Redirect return 重定向 Demo api TestRedirect response name
有时候在api执行完一个方法后,我们还得再跳到另外一个方法执行其他操作。
这种情况下,我们可以调用方法,也可以根据情况进行重定向作业
private void button2_Click(object sender, EventArgs e) { HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://localhost:20137/api/Demo?Name=张三&Gender=M&age=22"); request.Method = "GET"; //通过URL传值的话,可以不指定 contentType //request.ContentType = "text/html;charset=utf-8"; //request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); this.textBox1.Text = result; } } }
第一种 Redirect
public IHttpActionResult Get([FromUri]Person person) { return Redirect("http://localhost:20137/api/Demo/TestRedirect?name=张三");// 这种可以,不过action返回类型是IHttpActionResult //return Content<Person>(HttpStatusCode.BadRequest,person); } [HttpGet] [Route("api/Demo/TestRedirect")] public IHttpActionResult TestRedirect([FromUri]string name) { return Json("成功跳转"+ name); }
第二种 利用HttpReponseMessage
public HttpResponseMessage Get([FromUri]Person person) { var response = Request.CreateResponse(HttpStatusCode.Moved); response.Headers.Location = new Uri("http://localhost:20137/api/Demo/TestRedirect?name=张三"); response.Content = new ObjectContent(typeof(string), "重定向传值", new JsonMediaTypeFormatter()); return response;// 这种也可以,不过action 返回类型是 HttpResponseMessage } [HttpGet] [Route("api/Demo/TestRedirect")] public IHttpActionResult TestRedirect([FromUri]string name) { return Json("成功跳转"+ name); }
标签:WebApi,Redirect,return,重定向,Demo,api,TestRedirect,response,name 来源: https://www.cnblogs.com/JerryZhang320/p/15527107.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。