确定的URL对Ajax的WebMethod(在Sitefinity)Ajax、URL、Sitefinity、WebMethod

2023-09-10 20:22:07 作者:枝头花几许

我想弄清楚如何确定的baseUrl应该是什么这个AJAX的WebMethod POST。

根据该 Sitefinity线程的的baseUrl是路径Sitefinity项目文件夹。

在我的情况下,该路径应沿着线的东西:

C:\的Inetpub \网站\公,但这个路径是不相同的格式为MyWebForm.aspx / WebMethod的

这似乎是一个简单的问题,但在此之前,我去测试了这一点,我要确保我做了正确的事情。

我想AP preciate任何建议和反馈。

在此先感谢。

 函数buttonClick(五){
    即preventDefault();
    $阿贾克斯({
        键入:POST,
        网址:的baseUrl +MyWebForm.aspx /的WebMethod
        数据:1,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:
            功能() {
                警报(成功);
            },
        错误:
            函数(MSG){
                警报(对不起,发生了错误,请稍后重试);
            }
    });
}
 
基于jQuery的ajax对WebApi和OData的封装

解决方案

如果你在你的SitefinityWebApp根.aspx文件的地址可以是相对的,那么基础URL将是/。我建议把它变成像的WebMethods的文件夹,然后它会baseURL时将是/的WebMethods。我会建议使用一个MVC控制器,这一点我自己,甚至添加WebAPIController,你必须添加自定义溃败的引导程序,下面添加到您的Global.asax中。创建cotnroller,现在你可以调用/ API / myController的/ GetSomeStuff或/ API / myController的/ PostSomeStuff

 保护无效的Application_Start(对象发件人,EventArgs的)
{
    Bootstrapper.Initialized + =新的EventHandler< ExecutedEventArgs> (OnBootstrapperInitialized);
}
    私有静态无效OnBootstrapperInitialized(对象发件人,Telerik.Sitefinity.Data.ExecutedEventArgs E)
        {
    如果(e.CommandName ==自举)
    {
        的RegisterRoutes(RouteTable.Routes);
    }
}
私有静态无效的RegisterRoutes(RouteCollection路由)
{
    routes.Ignore({}资源个.axd / {* PATHINFO});

     routes.MapHttpRoute(
                 名称:ActionApi
                 routeTemplate:API / {控制器} / {行动} / {ID},
                 默认:新{n = RouteParameter.Optional}
                 );
}
 

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

解决方案

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 rout in the bootstrapper, Adding below to your Global.asax. Create a cotnroller 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 }
                 );
}