1

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');
            }
    });
}
terbubbs
  • 1,512
  • 2
  • 25
  • 48

2 Answers2

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 }
                 );
}
rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
Jon R.
  • 977
  • 6
  • 12
  • what are the benefits of using an MVC controller or WebAPI Controller? It seems like extra code, if the address can be simply be "/WebMethods/MyWebForm.aspx/WebMethod". I'm just curious because it's obvious I have a lot to learn. – terbubbs Apr 29 '15 at 16:51
  • 1
    If you use the WebApi, you only need to register the route once. Then any controller can be used. You can then have Multiple methods that don't require an aspx page which is not a straight forward solution. The WebApi Approach only requires 1 class file. Plus for sitefinity is a easy relative path from any page. http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api – Jon R. Apr 29 '15 at 18:17
  • that's a great explanation. thanks for all of your help. I'm pretty sure you helped me on my last question too lol.. – terbubbs Apr 29 '15 at 19:42
0
url : window.location.protocol + "//" + window.location.host + "/WebServices/MyWebForm.aspx/WebMethod";

Try this in URL field

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25