为 Ajax WebMethod 确定 url(在 Sitefinity 中)

Determining url for Ajax WebMethod (in Sitefinity)

我正在尝试弄清楚如何确定此 ajax 网络方法 POST.

的 baseUrl 应该是什么

根据这个Sitefinity thread,baseUrl 是 Sitefinity 项目文件夹的路径。

在我的例子中,路径应该是这样的:

"C:\inetpub\Website\Public" 但此路径的格式与 "MyWebForm.aspx/WebMethod"

不同

这似乎是一个简单的问题,但在我进行测试之前,我想确保我做的事情是正确的。

如果有任何建议和反馈,我将不胜感激。

提前致谢。

function buttonClick(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: baseUrl + "MyWebForm.aspx/WebMethod",
        data: 1,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:
            function() {
                alert('Success');
            },
        error:
            function (msg) {
                alert('Sorry, there was an error, please try again later');
            }
    });
}

如果您的 SitefinityWebApp 的根目录下有一个 .aspx 文件,地址可以是相对地址,然后基 URL 将是“/”。我建议将其放入 "WebMethods" 之类的文件夹中,然后 baseurl 将是“/WebMethods”。我建议自己为此使用 MVC 控制器,甚至添加 WebAPIController,您必须在引导程序中添加自定义路由,在下面添加到您的 Global.asax。创建一个控制器,现在你可以调用 /api/MyController/GetSomeStuff 或 /api/MyController/PostSomeStuff

protected void Application_Start(object sender, EventArgs e)
{
    Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>  (OnBootstrapperInitialized);
}
    private static void OnBootstrapperInitialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
    if (e.CommandName == "Bootstrapped")
    {
        RegisterRoutes(RouteTable.Routes);
    }
}
private static void RegisterRoutes(RouteCollection routes)
{
    routes.Ignore("{resource}.axd/{*pathInfo}");

     routes.MapHttpRoute(
                 name: "ActionApi",
                 routeTemplate: "api/{controller}/{action}/{id}",
                 defaults: new { id = RouteParameter.Optional }
                 );
}
url : window.location.protocol + "//" + window.location.host + "/WebServices/MyWebForm.aspx/WebMethod";

在 URL 字段中试试这个