【问题标题】:Json get 404 Not FoundJson 得到 404 未找到
【发布时间】:2012-03-15 06:06:22
【问题描述】:

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

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


    }

【问题讨论】:

  • 您知道您的托管服务提供商使用的是哪个版本的 IIS?您应该能够在 firebug 响应标头中看到它。
  • 他们使用的是 Microsoft-IIS/7.5
  • 使用 REST 客户端在 firefox 上测试您的服务,一旦您在那里工作,您可以尝试修复此代码!

标签: c# asp.net asp.net-mvc json silverlight


【解决方案1】:

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

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

【讨论】:

  • 问题是我不能使用 Url.Action() 辅助方法来生成查询,因为那是在 silverlight 项目中,您不能在其中引用 System.Web.Mvc?
  • 为什么不能在那里引用 System.Web.MVC?
  • 您不能将 System.Web.MVC dll 添加到 silverlight 项目。它只允许您添加 Silverlight 特定的 dll。
  • 您也不能在 JavaScript 文件中使用 Url.Action() 辅助方法。看看我是如何在blogs.msdn.com/b/rickandy/archive/2012/01/09/…
【解决方案2】:

在 Silverlight 中,您可以构建一个相对于托管 XAP 的页面的 URL:

Uri uri = new Uri(HtmlPage.Document.DocumentUri, "../getcountrydata/");

更好 - 相对于您的 XAP:

Uri uri = new Uri(App.Current.Host.Source.AbsoluteUri, "../getcountrydata/");

更多信息在这里:http://weblogs.asp.net/lduveau/archive/2009/03/13/get-silverlight-xap-and-hosting-page-url.aspx

【讨论】:

    【解决方案3】:

    Tanveer-Ibn- Haresh 是正确的 - 您提供的 URL 假定应用程序在根目录下运行。我的教程 [使用 DropDownList 框和 jQuery][1] [1]:http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc 展示了如何正确执行此操作。

    1. Fiddler 会立即向您展示问题。
    2. 您应该在部署之前使用 IIS 在本地进行测试。

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2018-10-27
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多