ASP. NET WebAPI Get request parameters through HttpContextBase

Source: Internet
Author: User

The request in Webapi is a httprequestmessage type and cannot have querystring and from methods to receive parameters like the Web tradition, whereas the traditional Httpreqest base class is Httpreqestbase

So here we're going to use it directly (httpcontextbase) request.properties["Ms_httpcontext"]

 Public void Post ([frombody]string  value) {    = (httpcontextbase) request.properties["ms_ HttpContext"]; // Get traditional context    Httprequestbase request = context. Request; // defining traditional Request objects                string name = Request. form["name"];    }

1. Get Traversal route parameters

// Get route parameters idictionary<stringobjectThisnew  StringBuilder (); foreach (var in dic) {    Builder. AppendFormat ("key:{0},value:{1}", item. Key, item. Value);    Builder. Appendline ();} return Builder. ToString ();

2. Traversing form parameters

////Get form parametersHttpContextBase context = (httpcontextbase) request.properties["Ms_httpcontext"];//Get Traditional contextHttprequestbase request = context. Request;//defining traditional Request ObjectsStringBuilder Builder =NewStringBuilder ();foreach(stringIteminchrequest. Form.keys) {Builder. AppendFormat ("Key:{0},value:{1}", item, request.    Form[item]); Builder. Appendline ();}returnBuilder. ToString ();

ASP. NET WebAPI Get request parameters through HttpContextBase

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.