将 Silverlight 与 ASP.NET MVC 结合使用时收到 WebException NotFound

发布于 2024-10-11 08:11:34 字数 1599 浏览 4 评论 0原文

我不完全确定如何解释这一点,但基本上我正在尝试使用托管在 ASP.NET MVC 应用程序中的 Silverlight 应用程序。我在 MVC 端有一个基本控制器,其中包含一个接受某些字符串参数并返回 ActionResult 的方法(对于此程序而言,该结果是一些 Json 数据)。

当我处理 Silverlight 控件中的 WebClient.OpenReadCompleted 事件时,出现了我的问题。当在此控件中调用 WebClient.OpenReadAsync 方法时,它会成功到达控制器,然后按预期向相关事件处理程序报告。然而,当它被处理时,事件参数包含一个错误,指出:“远程服务器返回错误:NotFound。”。

以前,我注意到这是由于我的通信 URL 的某些部分不正确而导致的 - 在这种情况下并非如此。通过一些谷歌搜索,我还注意到这是一个一般错误。因此,我相当困惑。更令人困惑的是,我在程序的另一部分使用了完全相同的通信尝试,它检索字符串数组,并且工作得很好。

请参阅下面的示例代码(由于该程序的性质,我无法发布完整的代码)。

Silverlight Control

WebClient mClient = new WebClient();

public void RequestData()
{
    mClient.OpenReadAsync(new Uri("http://localhost:51234/Home/GetData"));
    mClient.OpenReadCompleted += new OpenReadCompletedEventHandler(mClient_OpenReadCompleted);
}

private void mClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if(!e.Cancelled && e.Error == null) // <-- e.Error here is a WebException
    {
        var serializer = new DataContractJsonSerializer(typeof(Data));
        Data data = (Data)serializer.ReadObject(e.Result);
    }
}

MVC 控制器 - 名为 HomeController 并通过“Home”访问

public ActionResult GetData()
{
    return Json(new Data(), JsonRequestBehaviour.AllowGet);
}

注意 这里,Data包含三个类型的成员;字符串、字符串和字节数组。为了序列化的目的,我通过包含 get 和 set 部分的公共属性公开了所有三个成员。我还添加了一个不带参数的公共构造函数。

对此的任何建议将不胜感激。

非常感谢。

更新

我刚刚尝试了使用不同数据的相同代码,并且效果很好。我想知道这是否是数据的大小(因为第一次尝试是使用非常大的数据),但我不明白为什么如果调用成功击中控制器,这会很重要。

I'm not entirely sure how to explain this, but basically I am trying to use a Silverlight application hosted within an ASP.NET MVC application. I have a basic controller on the MVC side that contains a method which accepts some string parameter and returns an ActionResult (for the purpose of this program, that result is some Json data).

My problem arises when I handle the WebClient.OpenReadCompleted event within the Silverlight control. When the WebClient.OpenReadAsync method is called within this control, it successfully reaches the controller and then reports back to the relevant event handler as expected. However, when it is handled, the event arguments contain an error stating: "The remote server returned an error: NotFound.".

Previously, I have noticed this is caused when some part of my communication URL is incorrect - in this case it is not. From some Googling, I have also noticed that this is a generic error. As such, I'm rather stumped. To make matters more confusing, I use this exact same communication attempt in another part of the program, that retrieves an array of strings, and that works perfectly fine.

Please see the example code below (due to the nature of this program, I am unable to post the full code).

Silverlight Control

WebClient mClient = new WebClient();

public void RequestData()
{
    mClient.OpenReadAsync(new Uri("http://localhost:51234/Home/GetData"));
    mClient.OpenReadCompleted += new OpenReadCompletedEventHandler(mClient_OpenReadCompleted);
}

private void mClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if(!e.Cancelled && e.Error == null) // <-- e.Error here is a WebException
    {
        var serializer = new DataContractJsonSerializer(typeof(Data));
        Data data = (Data)serializer.ReadObject(e.Result);
    }
}

MVC Controller - named HomeController and accessed with "Home"

public ActionResult GetData()
{
    return Json(new Data(), JsonRequestBehaviour.AllowGet);
}

Note
Here, Data contains three members of types; string, string and byte array. For the purpose of serialization, I have exposed all three members through public properties containing both get and set parts. I have also added a public constructor taking no arguments.

Any advice on this would be greatly appreciated.

Many thanks in advance.

UPDATE

I've just tried the same bit of code with different data, and it works fine. I wondered if it was the size of the data (as the first attempt was with very large data), but I don't understand why that would matter if the call managed to hit the controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-10-18 08:11:34

如果您想查看真正的服务器端异常,这可能会帮助您:

http://msdn.microsoft.com/en-us/library/ee844556(v=VS.95).aspx

那里描述的任何一种方法都可能会说明真正的问题。

If you want to see the real server-side exception, this may help you:

http://msdn.microsoft.com/en-us/library/ee844556(v=VS.95).aspx

Either approach described there would probably illuminate the real problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文