Determining url for Ajax WebMethod (in Sitefinity)

走远了吗. 提交于 2019-12-11 13:34:25

问题


I'm trying to figure out how to determine what the baseUrl should be for this ajax webmethod POST.

According to this Sitefinity thread, the baseUrl is the path to the Sitefinity project folder.

In my case, the path should something along the lines of:

"C:\inetpub\Website\Public" but this path is not in the same format as "MyWebForm.aspx/WebMethod"

This seems like a simple problem but before I go testing this out, I want to make sure I'm doing the right thing.

I'd appreciate any suggestions and feedback.

Thanks in advance.

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');
            }
    });
}

回答1:


If you have an .aspx file at the root of your SitefinityWebApp the address can be relative and then base URL would be "/". I would recommend putting it into a folder like "WebMethods" then it would be baseurl would be "/WebMethods". I would recommend using an MVC controller for this myself or even adding a WebAPIController, you'll have to add a custom route in the bootstrapper, Adding below to your Global.asax. Create a controller and now you can call /api/MyController/GetSomeStuff or /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 }
                 );
}



回答2:


url : window.location.protocol + "//" + window.location.host + "/WebServices/MyWebForm.aspx/WebMethod";

Try this in URL field



来源:https://stackoverflow.com/questions/29948878/determining-url-for-ajax-webmethod-in-sitefinity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!