0

このリンクで例をテストしています:http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetmvc4_topic5#_Toc319061802しかし、WebClientを使用して別のコントローラーを呼び出すと500エラーが発生します。

「http:// localhost:2323 / photo / galleryを直接実行していますが、WebClientを使用してアクションを実行しようとすると、500エラーが返されますか?なぜですか?」にアクセスすると、

   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エラー:

[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
4

2 に答える 2

5

わかりにくいです。おそらく、呼び出しているコントローラーアクションには承認が必要ですか?またはセッションを使用しますか?WebClientリクエストを送信しても、クライアントから送信されたクライアントCookieはインデックスアクションに委任されません。

コードをデバッグして、サーバーから返される正確な応答を確認する方法は次のとおりです。

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
    }
}

また、問題がターゲットコントローラーのアクションが依存するASP.NETセッションに関連していることが判明した場合、リクエストを使用してクライアントCookieを委任する方法は次のとおりです。

WebClient client = new WebClient();
client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"];
于 2012-05-24T09:07:02.410 に答える
0

User-Agentヘッダーが原因でエラーが発生しました

解決策は次のとおりです。

public ActionResult Index()
    {
        WebClient client = new WebClient();
        client.Headers[HttpRequestHeader.UserAgent] = Request.Headers["User-Agent"];
        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);
    }
于 2012-05-24T11:01:58.497 に答える