我正在尝试使用这样的控制器将数据从 Web Api 发布到不同项目解决方案中的另一个 Web Api
public class PublishWebController : ApiController
{
static HttpClient client = new HttpClient();
static async Task<Uri> CreateArticleAsync(Article article)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/articles", article);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
void Main()
{
Post().GetAwaiter().GetResult();
}
//POST api/<controller>
[Mime]
public async Task<Article> Post()
{
client.BaseAddress = new Uri("http://localhost:64395/api/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var fileuploadPath = HttpContext.Current.Server.MapPath("~/uploads");
var multiFormDataStreamProvider = new MultiFileUploadProvider(fileuploadPath);
await Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);
string uploadingFileName = multiFormDataStreamProvider.FileData.Select(x => x.LocalFileName).FirstOrDefault();
return new Article
{
Title = HttpContext.Current.Request.Form["Title"],
ArticleContent = HttpContext.Current.Request.Form["ArticleContent"],
ImagePath = "~/Images/" + Path.GetFileName(uploadingFileName),
};
}
}
当我尝试使用 Postman 发送数据时,它向其 localhost 返回了一个值,但没有向我定位的 uri 返回一个值
我该如何解决这个问题?
请检查指定的 uri 是否正确。
请发现:
client.BaseAddress = new Uri("http://localhost:64395/api/");
和 await client.PostAsJsonAsync("api/articles", article);
给我们: http://localhost:64395/api/api/articles
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句