【问题标题】:Determining url for Ajax WebMethod (in Sitefinity)确定 Ajax WebMethod 的 url(在 Sitefinity 中)
【发布时间】:2015-07-09 01:01:26
【问题描述】:

我试图弄清楚如何确定这个 ajax webmethod 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');
            }
    });
}

【问题讨论】:

    标签: asp.net ajax json webmethod sitefinity


    【解决方案1】:

    如果您在 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 }
                     );
    }
    

    【讨论】:

    • 使用 MVC 控制器或 WebAPI 控制器有什么好处?如果地址可以简单地是“/WebMethods/MyWebForm.aspx/WebMethod”,这似乎是额外的代码。我只是好奇,因为很明显我还有很多东西要学。
    • 如果使用WebApi,只需要注册一次路由。然后可以使用任何控制器。然后,您可以拥有多种不需要 aspx 页面的方法,这不是一个直接的解决方案。 WebApi 方法只需要 1 个类文件。 Plus for sitefinity 是任何页面的简单相对路径。 asp.net/web-api/overview/getting-started-with-aspnet-web-api/…
    • 这是一个很好的解释。感谢您所有的帮助。我很确定你在我的最后一个问题上也帮助了我哈哈..
    【解决方案2】:
    url : window.location.protocol + "//" + window.location.host + "/WebServices/MyWebForm.aspx/WebMethod";
    

    在 URL 字段中试试这个

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多