【问题标题】:converting a asp.net mvc to a razor page将 asp.net mvc 转换为剃须刀页面
【发布时间】: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 它...

标签: asp.net razor


【解决方案1】:

例如使用处理程序

在你的cs文件上

 public IActionResult OnPostBarcode(string barcode)

在你的 js 上 var uri = "myPage/?handler=Barcode"

$.post( uri ,{barcode:barcode}, function( data ) {
 console.log(data)
});

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 2020-11-09
    • 2012-05-16
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2013-02-22
    • 2020-08-11
    • 1970-01-01
    相关资源
    最近更新 更多