我已经创建了一个ASP.NET核心API,并按如下方式定义了我的控制器:
[Route("api/[controller]")]
[ApiController]
public class SignUpController : ControllerBase
{
private readonly DNNContext _context;
public SignUpController(DNNContext context)
{
_context = context;
var merchantSignUpResponse = PutResponse();
}
[HttpPut("{id}")]
public async Task<IActionResult> PutResultAsync(int id, [FromBody]ProPaySignUp signUp)
{
try
{
await _context.SignUpResponses.AddAsync(PutResponse());
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Accepted();
}
public SignupResponse PutResponse()
{
string url = "https://xmltest.propay.com/api/propayapi/";
var credentials = GetCredentials();
var request = BuildMerchantData();
var restRequest = CreateRestRequest("/Signup", Method.PUT);
restRequest.AddJsonBody(request);
return Execute<SignupResponse>(restRequest, url);
}
private static RestRequest CreateRestRequest(string resource, Method method)
{
var credentials = GetCredentials();
var restRequest = new RestRequest
{
Resource = resource,
Method = method,
RequestFormat = DataFormat.Json,
};
restRequest.AddHeader("accept", "application/json");
restRequest.AddHeader("Authorization", credentials);
return restRequest;
}
private static T Execute<T>(IRestRequest request, string baseUrl) where T : class, new()
{
var client = new RestClient(baseUrl);
var response = client.Execute<T>(request);
if (response.ErrorException != null)
{
Console.WriteLine(
"Error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
response.ErrorException,
response.Headers,
response.Content,
response.StatusCode);
}
return response.Data;
}
解决方案成功构建。然后我启动Postman并输入URL
https://localhost:44381/api/SignUp
There was an error connecting to https://localhost:44381/api/SignUp.
然后,我在我的项目所在的目录下打开了一个命令提示符并执行了它
dotnet run SOBProPayService
. 返回的错误是
error NU1605: Detected package downgrade: Microsoft.AspNetCore.Razor.Design from 2.2.0 to 2.1.2. Reference the package directly from the project to select a different version.
如何解决此错误?