为什么返回500错误,而使用Web客户端调用另一个控制器? [英] Why return 500 error while calling another controller using WebClient?

查看:115
本文介绍了为什么返回500错误,而使用Web客户端调用另一个控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个链接测试例如:的http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetmvc4_topic5#_Toc319061802但我在使用Web客户端的500错误调用另一个控制器。

当我访问的http://本地主机:?2323 /照片/画廊直接运行,但我从行动使用的WebClient它返回500错误为什么试图

 公众的ActionResult指数()
    {
        Web客户端的客户端=新的WebClient();
        VAR响应= client.DownloadString(Url.Action(图库,照片,空,Request.Url.Scheme));
        VAR JSS =新的JavaScriptSerializer();
        VAR的结果= jss.Deserialize<名单,LT;照片及GT;>(响应);        返回视图(结果);
    }

由下面的异常创建500错误:

  [ArgumentNullException:值不能为空。
参数名:输入]
   System.Text.RegularEx pressions.Regex.Match(字符串输入)6411438
   Microsoft.VisualStudio.Web.Runtime.Tracing.UserAgentUtilities.GetEurekaVersion(字符串的userAgent)+79
   Microsoft.VisualStudio.Web.Runtime.Tracing.UserAgentUtilities.IsRequestFromEureka(字符串的userAgent)+36
   Microsoft.VisualStudio.Web.Runtime.Tracing.SelectionMappingExecutionListenerModule.OnBeginRequest(Object发件人,EventArgs五)+181
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器;放大器; completedSynchronously)+69


解决方案

这很难说。也许您呼叫控制器动作需要授权?或使用会话?当你把你的Web客户端请求时,它并没有委派任何人通过客户端发送到Index操作客户端的cookie。

下面是你如何能调试code和看到服务器返回的确切回应:

  Web客户端的客户端=新的WebClient();
尝试
{
    VAR响应= client.DownloadString(Url.Action(图库,照片,空,Request.Url.Scheme));
}
赶上(引发WebException前)
{
    使用(VAR读者=新的StreamReader(ex.Response.GetResponseStream()))
    {
        字符串的responseText = reader.ReadToEnd(); //< - 看看这里获取关于错误的更多细节
    }
}

如果事实证明你的目标控制器操作取决于问题与ASP.NET会话,这里是你如何能与委托请求的客户端的cookie:

  Web客户端的客户端=新的WebClient();
client.Headers [Htt的prequestHeader.Cookie] = Request.Headers [曲奇];

I'm testing example on this link: http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetmvc4_topic5#_Toc319061802 but I have 500 error calling another controller using WebClient.

When I access to "http://localhost:2323/photo/gallery directly is running, but I'm trying from action using WebClient it return 500 error? Why?"

   public ActionResult Index()
    {
        WebClient client = new WebClient();
        var response = client.DownloadString(Url.Action("gallery", "photo", null, Request.Url.Scheme));


        var jss = new JavaScriptSerializer();
        var result = jss.Deserialize<List<Photo>>(response);

        return View(result);
    }

500 error created by below exception:

[ArgumentNullException: Value cannot be null.
Parameter name: input]
   System.Text.RegularExpressions.Regex.Match(String input) +6411438
   Microsoft.VisualStudio.Web.Runtime.Tracing.UserAgentUtilities.GetEurekaVersion(String userAgent) +79
   Microsoft.VisualStudio.Web.Runtime.Tracing.UserAgentUtilities.IsRequestFromEureka(String userAgent) +36
   Microsoft.VisualStudio.Web.Runtime.Tracing.SelectionMappingExecutionListenerModule.OnBeginRequest(Object sender, EventArgs e) +181
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +69

解决方案

It's hard to tell. Maybe the controller action that you are calling requires authorization? Or uses session? When you send your WebClient request it doesn't delegate any of the client cookies that were sent by the client to the Index action.

Here's how you can debug your code and see the exact response returned by the server:

WebClient client = new WebClient();
try
{
    var response = client.DownloadString(Url.Action("gallery", "photo", null, Request.Url.Scheme));
}
catch (WebException ex)
{
    using (var reader = new StreamReader(ex.Response.GetResponseStream()))
    {
        string responseText = reader.ReadToEnd(); // <-- Look here to get more details about the error
    }
}

And if it turns out that the problem is related to the ASP.NET Session that your target controller action depends upon, here's how you could delegate the client cookies with the request:

WebClient client = new WebClient();
client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"];

这篇关于为什么返回500错误,而使用Web客户端调用另一个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆