ASP.NET - 使用多个不同的参数调用和处理Web API(ASP.NET - Call and handle Web API with multiple different parameters)
我是Web API的新手,我有一个无法解决的问题。
这是我的问题:如何调用需要多个参数的Web API(流或对象以及两个或三个字符串)? 如何处理Web API中的这些参数?
例如,我的Web API中有这个方法:
public class MyController : ApiController { [HttpPost] public MyObject Method(Stream s, string first, string second) { // take the parameters and do something } }
其中Stream是文件流(或其他情况下的对象)。 如何将所有这些参数添加到客户端请求正文? 如何从那里拿出来并在方法中使用?
编辑 :这个解决方案好吗? 这里的客户:
{ var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["first"] = "true"; queryString["second"] = "false"; var uri = "https://myapi.com/api/mycontroller/method?" + queryString; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes(myFile); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("<application/json >"); response = await client.PostAsync(uri, content); } }
这里的Web API:
[HttpPost] public MyObject Method([FromBody]Stream s, [FromUri]string first, [FromUri]string second) { //do something }
I am new to Web API and I have question that can not resolve.
This is my question: how call a Web API that need multiple parameters (stream or object and two or three string)? And how handle this parameters inside Web API?
For example, I have this method in my Web API:
public class MyController : ApiController { [HttpPost] public MyObject Method(Stream s, string first, string second) { // take the parameters and do something } }
Where Stream is a stream of file (or an object in other cases). How can add all these parameters to client request body? And how take them from there and use in the method?
EDIT: this solution is good? Here the client:
{ var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["first"] = "true"; queryString["second"] = "false"; var uri = "https://myapi.com/api/mycontroller/method?" + queryString; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes(myFile); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("<application/json >"); response = await client.PostAsync(uri, content); } }
And here the Web API:
[HttpPost] public MyObject Method([FromBody]Stream s, [FromUri]string first, [FromUri]string second) { //do something }
原文:https://stackoverflow.com/questions/40615590
最满意答案
试试这个代码:
{ "name": "SiteUrl", "type": "string", "label": "SiteUrl", "defaultValue": "", "required": true, "helpMarkDown": "The url to the site.", "visibleRule": "Wakeup = true", "groupName": "advanced" }
Try this code:
{ "name": "SiteUrl", "type": "string", "label": "SiteUrl", "defaultValue": "", "required": true, "helpMarkDown": "The url to the site.", "visibleRule": "Wakeup = true", "groupName": "advanced" }
相关问答
更多-
试试这个代码: { "name": "SiteUrl", "type": "string", "label": "SiteUrl", "defaultValue": "", "required": true, "helpMarkDown": "The url to the site.", "visibleRule": "Wakeup = true", "groupName": "advanced" } ...
-
有点像: $(':radio').change(function() { $('#B').prop('disabled', !(this.value == '2' && this.checked)); }); 演示: http : //jsfiddle.net/5PMLr/ 旁注:您应该有多个ID用于ID,而不是以数字开头。 Something in the line of: $(':r ...
-
Oozie允许你使用EL表达式,其中包括条件操作符?: 。 这使得在指定的路径不存在时实现默认路径相当简单: ${fs:exists(specified_path) ? specified_path : default_path} Oozie allows you to use EL expressions, which includes the conditional operator ?:. This makes it fairly straight-forward to implement a de ...
-
您可以在UI中定义滑块,并在需要时在服务器中更新它们: library(shiny) ui =pageWithSidebar( headerPanel("Glass fullness"), sidebarPanel( sliderInput(inputId = "Full", label = "% water", min = 0, max = 1, value = 0.2), sliderInput(inputId = "Empty", label = "% air", min = ...
-
你应该使用uiOutput和renderUI来生成依赖于输入的小部件: shinyApp( ui = fluidPage( textInput("A", "Enter a string"), uiOutput("B_ui"), textOutput("curval") ), server = function(input, output) { output$B_ui <- renderUI({ if (input$A=="foo") textInput ...
-
我认为最好的方法是检查哪些是重点的视图,以避免事件的循环传播: RxTextView.textChangeEvents(mEditText1) .filter(textViewTextChangeEvent -> mEditText1.hasFocus()) .subscribe(textViewTextChangeEvent -> { updateEditText(mEditText2, calculateText2(textViewTextChangeEvent.text ...
-
我通过将数据集(即我的动态查询的结果)转换为CSV来实现它,其中第一个记录具有字段的名称和以下记录作为实际记录。 现在我可以返回始终是STRING的CSV,然后将其转换为前端的表格 I got it working by converting the dataset, i.e. the result of my dynamic query, into a CSV with the first record having the names of the fields and the following re ...
-
如何在R中渲染不同的绘图输出类型取决于用户输入?(How to render different plot output types in R Shiny dependent of user input?)[2022-09-16]
类似的东西。 在server : output$plot <- renderPlot({ validate(need(input$pType=="A", message=FALSE)) hist(myData) }) output$dygraph <- renderDygraph({ validate(need(input$pType=="B", message=FALSE)) barplot(myData) }) 在ui : conditionalPanel('input.pType== ... -
闪亮的iframe取决于输入(Shiny iframe depending on input)[2022-03-17]
使用renderUI和htmlOutput来构造iframe而不是renderText ,还有很多错误。 下面是代码的简化版本(只有你的第一个链接给出了一张图片): ui.R shinyUI(fluidPage(titlePanel("Choose your preferred name"), sidebarLayout( sidebarPanel( fluidRow( ... -
如何在VSTS构建任务输入PickList中显示“区域路径”?(How to display “Area path” in VSTS build task input PickList?)[2023-07-31]
是的。 您可以在task.josn文件中添加以下部分来实现此目的: "inputs": [ { "name": "rootArea", "type": "pickList", "label": "rootArea", "defaultValue": "", "required": false, "helpMarkDown": "Select the root area.", "properties": { ...