简体   繁体   English

未找到 API Controller - ASP.NET

[英]API Controller not being found - ASP.NET

Bit of background:一点背景:

I'm adding a feature to one of our clunky old systems to keep it ticking over until such time as the new version of the system is at a level where this feature could be brought to a level where it can support this feature.我正在向我们的一个笨重的旧系统添加一项功能,以使其保持运行,直到系统的新版本达到可以将该功能提升到可以支持该功能的水平为止。

The system is a combination of C# and VB (yes, I know, I'm slowly phasing the VB out).该系统是 C# 和 VB 的组合(是的,我知道,我正在慢慢淘汰 VB)。 Essentially, I'm adding this simple API Controller (there will be one or two more methods once finished, this is just me getting the first bit working):本质上,我添加了这个简单的 API Controller (一旦完成,将会有一个或两个更多的方法,这只是我开始工作的第一步):

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;


[RoutePrefix("/Utilities/LaptopTrolleyBooking/LaptopAPI/")]
public class LaptopBookingsController : ApiController
{
    [Route("GetLaptopBookings"), HttpGet]
    public async Task<HttpResponseMessage> GetLaptopBookings()
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NGConnectionString"].ConnectionString))
        {
            connection.Open();
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "FR_TT_GetUpcomingLaptopBookingsForJson";
                command.CommandType = CommandType.StoredProcedure;

                var result = await command.ExecuteScalarAsync();
                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(result.ToString(), Encoding.UTF8, "application/json");
                return response;
            }
        }
    }
}

However, when I attempt to call it from this javascript block:但是,当我尝试从此 javascript 块调用它时:

$(document).ready(function () {
    init();

    function createElement(tag) {
        return $('<' + tag + '>');
    }

    function createElement(tag, className) {
        return $('<' + tag + '>').addClass(className);
    }

    function init() {
        var headerElem = createElement('div', 'panHeader')
            .text('Laptop Trolley Bookings');

        $('.content')
            .append(headerElem);

        GetBookings();

        function GetBookings() {
            $.ajax({
                url: '/Utilities/LaptopTrolleyBooking/LaptopApi/GetLaptopBookings',
                method: 'get',
                contentType: 'json',
                dataType: 'json',
                success: function (response) {
                    debugger;
                },
                error: function () {
                    debugger;
                }
            });
        }
    }
});

I'm receiving an error 404. Have I done something obviously wrong here, or am I missing some setting somewhere?我收到错误 404。我在这里做了什么明显错误的事情,还是我在某处遗漏了一些设置?

If it helps, here's the Global.asax如果有帮助,这里是 Global.asax

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup

        ' License Aspose.Words & Aspose.PDF
        Dim licenseWords As New Aspose.Words.License()
        licenseWords.SetLicense("Aspose.Total.lic")
        Dim licensePDF As New Aspose.Pdf.License()
        licensePDF.SetLicense("Aspose.Total.lic")
        Dim licenseOCR As New Aspose.OCR.License()
        licenseOCR.SetLicense("Aspose.Total.lic")
        Dim licenseBarCode As New Aspose.BarCode.License()
        licenseBarCode.SetLicense("Aspose.Total.lic")
        Dim licenseBarCodeRecognition As New Aspose.BarCodeRecognition.License()
        licenseBarCodeRecognition.SetLicense("Aspose.Total.lic")
        Dim licenceCells As New Aspose.Cells.License()
        licenceCells.SetLicense("Aspose.Total.lic")



    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Get session data
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
    End Sub

</script>

After much research and further experimentation, I've found the issue.经过大量的研究和进一步的实验,我发现了这个问题。 In essence, the codebase was so old that it just doesn't support having an API controller within the web project.从本质上讲,代码库太旧了,以至于它不支持在 web 项目中包含 API controller。 To get around this I created a WebService, as such:为了解决这个问题,我创建了一个 WebService,如下所示:

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

[WebService(Namespace = "mySillyUrl")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class LaptopBookingController : WebService
{
    public LaptopBookingController()
    {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public void CancelLaptopBooking(int RSBH_ISN)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NGConnectionString"].ConnectionString))
        {
            connection.Open();
            using (SqlCommand command = connection.CreateCommand())
            {
                List<LaptopBooking> bookings = new List<LaptopBooking>();
                command.CommandText = "FR_TT_CancelLaptopBooking";
                command.Parameters.AddWithValue("RSBH_ISN", RSBH_ISN);
                command.CommandType = CommandType.StoredProcedure;

                var unused = command.ExecuteScalar();
            }
        }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetLaptopBookings()
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NGConnectionString"].ConnectionString))
        {
            connection.Open();
            using (SqlCommand command = connection.CreateCommand())
            {
                List<LaptopBooking> bookings = new List<LaptopBooking>();
                command.CommandText = "FR_TT_GetUpcomingLaptopBookingsForJson";
                command.CommandType = CommandType.StoredProcedure;

                var result = command.ExecuteScalar();
                var jArr = JArray.Parse(result.ToString());

                foreach (JObject jObj in jArr)
                {
                    bookings.Add(new LaptopBooking(jObj));
                }
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Context.Response.Write(serializer.Serialize(bookings));
            }
        }
    }
}

Then, from the javascript, the call was as simple as:然后,从 javascript 开始,调用很简单:

    function GetBookings() {
        $.ajax({
            url: '/Utilities/LaptopTrolleyBooking/LaptopBookingController.asmx/GetLaptopBookings',
            method: 'POST',
            //contentType: "application/json; charset=utf-8",
            //dataType: "json",
            data: '{ }',
            success: function (response) {
                //debugger;
                bookings = JSON.parse(response);

                ShowBookings();
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM