将asp.net mvc转换为razor页面
我有一个asp.net web应用程序,它使用以下代码调用控制器操作:将asp.net mvc转换为razor页面,asp.net,razor,Asp.net,Razor,我有一个asp.net web应用程序,它使用以下代码调用控制器操作: $(function () { $("#barcode").on("change", function (e) { // get the current value var barcode = $('#barcode').val(); // if there's no text, ignore the event
$(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核心应用程序,具有razor页面,例如使用处理程序 在你的cs文件上
public IActionResult OnPostBarcode(string barcode)
在你的js上
var uri=“myPage/?handler=条形码”
显示到目前为止的转换。很可能您需要使用
@Url.Page
我不确定从控制器将代码放在何处,这是否会作为一种方法放在.cshtml.cs文件中。我在该文件中有一个名为public void processScan(){…的方法,但不确定如何实际调用它。。。
$.post( uri ,{barcode:barcode}, function( data ) {
console.log(data)
});