从表单中获取信息以调用 API

9 浏览
0 Comments

从表单中获取信息以调用 API

我正在尝试使用MVC和Razor Pages在.NET Core中调用一个API。作为一个.NET Core的新学习者,我对MVC的工作方法感到困惑。我正在尝试从用户信息中填写一个表单,并使用提交按钮将该信息放入请求体中,然后通过控制器来访问该端点。到目前为止,我的代码如下所示:

***View/ Index.cshtml***

@using PTCConnector.Models.DB

@{

ViewData["Title"] = SharedLocalizer["Users"];

var user = ViewData["User"] as List;

List filePaths = ViewData["FilePaths"] as List;

}

用户名:

密码:

输入新密码:

***Models/ wh_adminLoginModel.cs***

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using PTCConnector.Views;

namespace PTCConnector.Areas.Whatsapp.Models

{

public class wh_adminLoginModel

{

public string df_username = "admin";

public string df_password = "helloWorld";

public string new_password { get; set; }

}

}

***Controller/ AuthAdminController.cs***

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using PTCConnector.Data;

using PTCConnector.Areas.Settings.Models;

using Microsoft.AspNetCore.Mvc.Rendering;

using PTCConnector.Models.DB;

using Microsoft.EntityFrameworkCore;

using Microsoft.AspNetCore.Authorization;

using PTCConnector.Areas.Whatsapp.Models;

using System.Net.Http;

using System.Text;

using PTCConnector.Areas.Whatsapp.Models;

namespace PTCConnector.Areas.Whatsapp.Controllers

{

[Area("Whatsapp")]

[TypeFilter(typeof(AdminActionFilter))]

[Authorize(Roles = "Admin")]

public class WhAuthController : Controller

{

public wh_adminLoginModel whLogin = new wh_adminLoginModel();

public async Task Login()

{

HttpClientHandler clientHandler = new HttpClientHandler

{

ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }

};

var client = new HttpClient(clientHandler);

byte[] bytes = Encoding.UTF8.GetBytes($"{whLogin.df_username}:{whLogin.df_password}");

var Base64Credentials = Convert.ToBase64String(bytes);

System.Diagnostics.Debug.WriteLine(Base64Credentials);

// 将Base64Credentials设置为带有前缀`Basic`的Authorization头

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Base64Credentials);

// 一些用于POST的数据,可以自由更改!

var data = new Dictionary

{

// 这个new_password应该来自用户视图中的模型

{ "new_password", $"{whLogin.new_password}" }

};

System.Diagnostics.Debug.WriteLine(data.Values);

Console.WriteLine("here 1");

// 将数据编码为FORM URL数据

var content = new FormUrlEncodedContent(data);

// 使用客户端进行异步POST调用。参数是URL和要POST的数据!

var response = await client.PostAsync("https://localhost:9090/v1/users/login", content);

// 异步!! - 将响应内容读取为字符串

var responseString = await response.Content.ReadAsStringAsync();

// 打印出响应字符串以进行调试!

//Console.WriteLine(responseString);

System.Diagnostics.Debug.WriteLine(responseString);

System.Diagnostics.Debug.WriteLine("Check");

Console.WriteLine("CheckNow");

}

}

}

0
0 Comments

问题的原因是默认情况下,在MVC中,所有未标记的方法都被视为GET方法。如果要将数据以POST形式发送回服务器,则需要通过添加HttpPost方法属性来将方法声明为POST方法。

解决方法是在方法上方添加HttpPost属性,并确保按照方法的签名(通过传递有效的模型类型)从表单中传入数据。

需要创建一个名为LoginViewModel.cs的类,在一个新的ViewModels文件夹下,该类包含UsernamePasswordNewPassword三个变量作为类的属性。

使用这个设置,当你在表单中输入信息并点击提交时,它应该能够到达后端并处理模型。

如果你想让这个过程完全独立,你可以开始使用Ajax来构建你的调用,并通过Json来进行请求。但我觉得这是一个全新的问题。

关于client.PostAsync查询的问题,你可以参考这个答案中的链接来获取相关信息。抱歉我不能提供更多帮助。

0