Skip to main content
 首页 » 编程设计

c#之Json 获取 404 未找到

2024年02月23日14langtianya

在 asp.net mvc 应用程序中,我有一个将 JsonResult 返回到 View 的方法。它在我的本地机器上完美运行,但是当应用程序部署在 Web 托管服务器上时,当我尝试通过点击 View 的链接获取此数据时,我在 Firebug 中收到 404 Not Found。有谁知道发生这种情况的可能原因吗?我如何生成路径的代码片段如下:

    private void get_info() 
    { 
        var serviceUri = new Uri("/getcountrydata/" + country_name + "/" + arms[0].Name + "/" + arms[1].Name + "/" + arms[2].Name + "/" + arms[3].Name, UriKind.Relative); 
        var webClient = new WebClient(); 
        webClient.OpenReadCompleted += openReadCompleted; 
        webClient.OpenReadAsync(serviceUri); 
    } 

下面的 Global.asax 路由:

routes.MapRoute( 
           "getcountrydata", 
           "getcountrydata/{country}/{indicator1}/{indicator2}/{indicator3}/{indicator4}", 
           new { controller = "Home", action = "getcountrydata" } 
       ); 

getcountrydata方法如下:

public JsonResult getcountrydata(string country, string indicator1, string indicator2, string indicator3, string indicator4) 
    { 
 
       LegoData legoData = captainClimateRepostory.GetLegoData(country, indicator1, indicator2, indicator3, indicator4); 
 
       DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LegoData)); 
       MemoryStream ms = new MemoryStream(); 
       ser.WriteObject(ms, legoData); 
 
       return Json(ms.ToArray(), JsonRequestBehavior.AllowGet); 
 
 
    } 

请您参考如下方法:

我认为您为操作提供的网址有问题。它与主机服务器无关。检查这个 SO 帖子..

jquery ajax call to JsonResult controller method results in 404 on IIS6