使用 Axios.Get 参数和 headers 传输到 WebApi 控制器 - 传输数据失败
Use Axios.Get with params and headers in transfer to WebApi Controller -Transfer data Failed
您好,我正在使用 webapi 开发 React。我在使用下面给出的代码 headers 和 parameters.see 将数据传输到 webapi 控制器时遇到问题。
反应 Js
import React from 'react';
import axios from 'axios';
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
componentDidMount() {
var access_token = "12121212121";
var username = "Test";
var guid= "W3432";
let config = {
headers: { Authorization: access_token,Username:username } ,
params: {guid}
}
axios.get('http://localhost:XXXX/Api/Authenticate/Getprofiledetails', config).then(response => {
console.log(response.data)
})
.catch(function (error) {
console.log(error);
})
}
}
控制器(Web Api2)
public class AuthenticateController : ApiController
{
[Route("Api/Authenticate/Getprofiledetails/{id}")]
[HttpGet]
public IHttpActionResult Getprofiledetails(Guid id)
{
try
{
return Ok("Done");
}
catch (Exception ex)
{
throw ex;
}
}}
Webapi.config
config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
错误
Message: "No HTTP resource was found that matches the request URI 'http://localhost:XXXX/Api/Authenticate/Getprofiledetails?guid=W3432'."
MessageDetail: "No action was found on the controller 'Authenticate' that matches the name 'Getprofiledetails'."
看起来您的网络中没有合适的控制器 api 来接收此类请求。
您应该将 id 作为 url 的一部分:
'http://localhost:XXXX/Api/Authenticate/Getprofiledetails/W3432'
更改代码:
componentDidMount() {
var access_token = localStorage.getItem('access_token');
var username = localStorage.getItem('user');
var guid= localStorage.getItem('Guiid');
axios.get('http://localhost:XXXX/Api/Authenticate/Getprofiledetails'+'/'+ guid, { headers: { Authorization: access_token,Username:username } }).then(response => {
console.log(response.data)
})
.catch(function (error) {
console.log(error);
})
}
您好,我正在使用 webapi 开发 React。我在使用下面给出的代码 headers 和 parameters.see 将数据传输到 webapi 控制器时遇到问题。
反应 Js
import React from 'react';
import axios from 'axios';
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
componentDidMount() {
var access_token = "12121212121";
var username = "Test";
var guid= "W3432";
let config = {
headers: { Authorization: access_token,Username:username } ,
params: {guid}
}
axios.get('http://localhost:XXXX/Api/Authenticate/Getprofiledetails', config).then(response => {
console.log(response.data)
})
.catch(function (error) {
console.log(error);
})
}
}
控制器(Web Api2)
public class AuthenticateController : ApiController
{
[Route("Api/Authenticate/Getprofiledetails/{id}")]
[HttpGet]
public IHttpActionResult Getprofiledetails(Guid id)
{
try
{
return Ok("Done");
}
catch (Exception ex)
{
throw ex;
}
}}
Webapi.config
config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
错误
Message: "No HTTP resource was found that matches the request URI 'http://localhost:XXXX/Api/Authenticate/Getprofiledetails?guid=W3432'." MessageDetail: "No action was found on the controller 'Authenticate' that matches the name 'Getprofiledetails'."
看起来您的网络中没有合适的控制器 api 来接收此类请求。 您应该将 id 作为 url 的一部分: 'http://localhost:XXXX/Api/Authenticate/Getprofiledetails/W3432'
更改代码:
componentDidMount() {
var access_token = localStorage.getItem('access_token');
var username = localStorage.getItem('user');
var guid= localStorage.getItem('Guiid');
axios.get('http://localhost:XXXX/Api/Authenticate/Getprofiledetails'+'/'+ guid, { headers: { Authorization: access_token,Username:username } }).then(response => {
console.log(response.data)
})
.catch(function (error) {
console.log(error);
})
}