2014-03-05 36 views
1

我正在使用Sitecore 7.1和一個MVC 4項目。Sitecore 7 MVC 4&Ajax調用

我正在試圖製作一個搜索框,它將搜索新聞到一個存儲桶並以JSON形式返回結果。

所以我讀了博客文章:http://keeplearningandsharing.wordpress.com/2013/12/13/sitecore-mvc-with-ajax/

,並嘗試做相同的,但它不工作。

我做了什麼:

1)我創建了一個名爲NewsOverviewController控制器這裏我把兩種方法

ActionResult Start() (HttpGet) which return my view with the searchbox 
JsonResult Starts(InpurtParams) which perform the search 

2)在Sitecore的,我建立了我的控制器渲染和動作開始添加到它。

3)我登記RouteConfig.cs一條新的路線(我把它放在Sitecore的路線之前和之後,並沒有改變anythings)

routes.MapRoute(
      name: "search", 
      url: "api/NewsOverview/{action}/{id}", 
      defaults: new { controller = "NewsOverview", action = "Starts", id = UrlParameter.Optional } 
     ); 

4)我把我的觀點如下代碼:

<script> 
function getSearchResult() { 
    var inputparams = {}; 
    inputparams.SearchTerm = document.getElementById("searchTerm").value; 

    try { 
     $.getJSON("/api/NewsOverview/starts", inputparams, displaySearchResult); 
    } 
    catch (ex) { 
     alert("Error : " + ex.message); 
    } 
} 

function displaySearchResult(data) { 
    $.each(data, function (i, result) { 
     $("#searchresult").append(result.Name + " - " + result.Url + "<br/>"); 
    }); 

}; 
</script> 

<div> 
    <input type="text" name="searchTerm" id="searchTerm" value="test"/> 
    <input type="button" onclick="getSearchResult();"/> 
    <div id="searchresult"></div> 
</div> 

我的問題,當我嘗試使用搜索: 他沒有找到URL(404(未找到)): (東道國)/ API/NewsOverview /啓動SEARCHTERM =測試

我是很新,MVC,所以我錯過,也許有些事情......

回答

0

我們在Application_Start事件中註冊路由,而不是在任何管道中註冊路由。它對我們運作良好。

protected void Application_Start() 
    { 
    RegisterRoutes(RouteTable.Routes); 
    } 

    private void RegisterRoutes(RouteCollection routes) 
    { 
    routes.MapRoute("NameOfRoute", "Path To Use from Ajax", new { controller = "Controller Class,Assembly", action = "Name Of Method On Controller" }); 
    }