【发布时间】:2018-06-08 12:24:14
【问题描述】:
我有一个 asp.net 网络应用程序,它使用以下代码调用控制器操作:
$(function () {
$("#barcode").on("change", function (e) {
// get the current value
var barcode = $('#barcode').val();
// if there's no text, ignore the event
if (!barcode) {
return;
}
// clear the textbox
$("#barcode").val("");
// var holdit = $('#textArea1').val();
$('#textArea1').val($('#textArea1').val() +' '+ barcode);
// post the data using AJAX
$.post('@Url.Action("scanned", "receiveScan")?barcode=' + barcode);
});
})
控制器:
[Produces("application/json")]
[Route("api/receiveScan")]
public class receiveScanController : Controller
{
private static readonly HttpClient client = new HttpClient();
public ActionResult scanned(string barcode)
{
var test = barcode;
receiveScanModel newScan = new receiveScanModel();
newScan.Barcode = barcode;
newScan.companyNo = 1;
string jsonScan = JsonConvert.SerializeObject(newScan, Formatting.Indented);
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://notarealservicehere.azurewebsites.net//api/receivescan");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(jsonScan);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
return Ok();
尝试转换为我的第一个 Razor 页面,一切正常,但 $.post 部分除外(显然)...
这会去哪里?
这是一个带有剃须刀页面的 asp.net 核心应用程序
【问题讨论】:
-
显示到目前为止的转换。您很可能需要使用
@Url.Page -
我不确定将控制器中的代码放在哪里,是否会作为方法放入 .cshtml.cs 文件中。我在该文件中有一个名为 public void processScan() {... 但只是不确定如何实际 call 它...