Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 确定Ajax WebMethod的url(在Sitefinity中)_Asp.net_Ajax_Json_Webmethod_Sitefinity - Fatal编程技术网

Asp.net 确定Ajax WebMethod的url(在Sitefinity中)

Asp.net 确定Ajax WebMethod的url(在Sitefinity中),asp.net,ajax,json,webmethod,sitefinity,Asp.net,Ajax,Json,Webmethod,Sitefinity,我试图找出如何确定这个ajax webmethod帖子的baseUrl应该是什么 据此,baseUrl是指向Sitefinity项目文件夹的路径 在我的例子中,路径应该是沿着以下路线: “C:\inetpub\Website\Public”,但此路径的格式与“MyWebForm.aspx/WebMethod”不同 这似乎是一个简单的问题,但在我开始测试之前,我想确保我做的事情是正确的 如果您有任何建议和反馈,我将不胜感激 提前谢谢 function buttonClick(e) { e.

我试图找出如何确定这个ajax webmethod帖子的baseUrl应该是什么

据此,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/getsomething或/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 }
                 );
}
受保护的无效应用程序\u启动(对象发送方,事件参数e)
{
Bootstrapper.Initialized+=新事件处理程序(OnBootstrapperInitialized);
}
私有静态无效OnBootstrapperInitialized(对象发送方,Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
如果(e.CommandName==“引导”)
{
注册地址(RouteTable.Routes);
}
}
专用静态无效注册表项(路由收集路由)
{
忽略(“{resource}.axd/{*pathInfo}”);
routes.MapHttpRoute(
名称:“ActionApi”,
routeTemplate:“api/{controller}/{action}/{id}”,
默认值:新建{id=RouteParameter.Optional}
);
}

在URL字段中尝试此操作

使用MVC控制器或WebAPI控制器的好处是什么?如果地址可以是“/WebMethods/MyWebForm.aspx/WebMethod”,那么它看起来像是额外的代码。我只是好奇,因为很明显我还有很多东西要学。如果你使用WebApi,你只需要注册一次路由。然后可以使用任何控制器。然后,您可以有多个不需要aspx页面的方法,而aspx页面不是一个直接的解决方案。WebApi方法只需要1个类文件。另外,对于sitefinity来说,从任何页面都可以轻松找到相对路径。这是一个很好的解释。谢谢你的帮助。我很确定你在我的最后一个问题上也帮了我,哈哈。。
url : window.location.protocol + "//" + window.location.host + "/WebServices/MyWebForm.aspx/WebMethod";