为什么我的 Razor ASP.NET、C# HTTPRequests return Response 的 body 不会?
Why won't my Razor, ASP.NET, C# HTTPRequests return the body of the Response?
我的最终目标是让 reCaptcha 在我的 Razor 应用程序中工作,但我被卡住了。我已经到了可以取回响应 Headers 的地步,但不知道如何取回实际的 JSON 响应 body。如果您需要更多信息,请告诉我。
在Startup.cs---------------------------------
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
在模型中 > Voter.cs --------------------------------
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Models
{
public class Voter
{
[Required(ErrorMessage = "Data is required.")]
public string reCaptchaPost { get; set; }
public string Result { get; set; }
}
}
在Vote2.cshtml.cs---------------------------------
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using rethianIdeas.Models;
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Pages.Ideas
{
public class Vote2Model : PageModel
{
[BindProperty]
public Voter reCaptchaTest { get; set; }
[BindProperty(Name = "g-recaptcha-response")]
[Required(ErrorMessage = "No One Beats Reacaptcha!")]
public string reCaptchaG { get; set; }
public IActionResult OnGet()
{
if (reCaptchaTest == null)
{
reCaptchaTest = new Voter();
}
return Page();
}
private readonly IHttpClientFactory _clientFactory;
public Vote2Model(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=6LcUFJUUAAAAADvuNOTGONNAGETITiFgNNG0sxBg&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var Client = _clientFactory.CreateClient().SendAsync(reCaptchaRequest);
reCaptchaTest.Result = Client.Result.ToString();
}
return Page();
}
}
}
在Vote2.cshtml---------------------------------
@page
@model rethianIdeas.Pages.Ideas.Vote2Model
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form method="post">
<div asp-validation-summary="All"></div>
<div>
<div>
<label asp-for="@Model.reCaptchaTest.reCaptchaPost"></label>
<input asp-for="@Model.reCaptchaTest.reCaptchaPost" />
<span asp-validation-for="@Model.reCaptchaTest.reCaptchaPost"></span>
</div>
</div>
<div class="g-recaptcha" data-sitekey="6LcUFJUUAAAAANOTGETTINGIT0SYj77GZHEhz73p0Q6_m"></div>
<div>
<input type="submit" value="Submit">
</div>
<div><label>Result: @Model.reCaptchaTest.Result</label></div>
</form>
Returns--------------------------------
Result: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Date: Fri, 08 Mar 2019 06:02:16 GMT Cache-Control: max-age=0, private X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Server: GSE Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39" Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Expires: Fri, 08 Mar 2019 06:02:16 GMT }
我根本不知道如何从这个东西中得到请求的 body。请帮助。
您正在调用一个异步方法,但没有等待 (await
) 它 return。
这应该会让您走上正确的道路。
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=***&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(reCaptchaRequest);
if (response.IsSuccessStatusCode)
{
reCaptchaTest.Result = await response.Content.ReadAsStringAsync();
}
else
{
// Errors, do something...
}
}
return Page();
}
您可以参考 ASP.NET Core Http.Client docs for more info. Also, here 是一个博客 post 涵盖使用 ASP.NET Core 和 reCaptcha。
我的最终目标是让 reCaptcha 在我的 Razor 应用程序中工作,但我被卡住了。我已经到了可以取回响应 Headers 的地步,但不知道如何取回实际的 JSON 响应 body。如果您需要更多信息,请告诉我。
在Startup.cs---------------------------------
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
在模型中 > Voter.cs --------------------------------
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Models
{
public class Voter
{
[Required(ErrorMessage = "Data is required.")]
public string reCaptchaPost { get; set; }
public string Result { get; set; }
}
}
在Vote2.cshtml.cs---------------------------------
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using rethianIdeas.Models;
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Pages.Ideas
{
public class Vote2Model : PageModel
{
[BindProperty]
public Voter reCaptchaTest { get; set; }
[BindProperty(Name = "g-recaptcha-response")]
[Required(ErrorMessage = "No One Beats Reacaptcha!")]
public string reCaptchaG { get; set; }
public IActionResult OnGet()
{
if (reCaptchaTest == null)
{
reCaptchaTest = new Voter();
}
return Page();
}
private readonly IHttpClientFactory _clientFactory;
public Vote2Model(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=6LcUFJUUAAAAADvuNOTGONNAGETITiFgNNG0sxBg&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var Client = _clientFactory.CreateClient().SendAsync(reCaptchaRequest);
reCaptchaTest.Result = Client.Result.ToString();
}
return Page();
}
}
}
在Vote2.cshtml---------------------------------
@page
@model rethianIdeas.Pages.Ideas.Vote2Model
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form method="post">
<div asp-validation-summary="All"></div>
<div>
<div>
<label asp-for="@Model.reCaptchaTest.reCaptchaPost"></label>
<input asp-for="@Model.reCaptchaTest.reCaptchaPost" />
<span asp-validation-for="@Model.reCaptchaTest.reCaptchaPost"></span>
</div>
</div>
<div class="g-recaptcha" data-sitekey="6LcUFJUUAAAAANOTGETTINGIT0SYj77GZHEhz73p0Q6_m"></div>
<div>
<input type="submit" value="Submit">
</div>
<div><label>Result: @Model.reCaptchaTest.Result</label></div>
</form>
Returns--------------------------------
Result: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Date: Fri, 08 Mar 2019 06:02:16 GMT Cache-Control: max-age=0, private X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Server: GSE Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39" Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Expires: Fri, 08 Mar 2019 06:02:16 GMT }
我根本不知道如何从这个东西中得到请求的 body。请帮助。
您正在调用一个异步方法,但没有等待 (await
) 它 return。
这应该会让您走上正确的道路。
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=***&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(reCaptchaRequest);
if (response.IsSuccessStatusCode)
{
reCaptchaTest.Result = await response.Content.ReadAsStringAsync();
}
else
{
// Errors, do something...
}
}
return Page();
}
您可以参考 ASP.NET Core Http.Client docs for more info. Also, here 是一个博客 post 涵盖使用 ASP.NET Core 和 reCaptcha。