oliverdejohnson
oliverdejohnson

Reputation: 1620

How do i display the response(html) from webclient in my asp.net mvc view

I'm faking a form post using webclient like this:

public static string SendHttpRequest(string url, string method, List<WebParameter> paramaters)
    {
        using(WebClient client = new WebClient())
        {

            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
              foreach (var param in paramaters)
              {
                  reqparm.Add(param.name, param.value);
                  //reqparm.Add("param2", "escaping is already handled");
              }
                byte[] responsebytes = client.UploadValues(url, method, reqparm);
              string responsebody = Encoding.UTF8.GetString(responsebytes);

              return responsebody;
       }
    }

Its working well so far but the response is an html page. i want to embed this page inside my view.

My controller action looks like this

        string response = SendHttpRequest(purl,"POST",param);

        ViewBag.Response = response;
        return View();

In my view when i called ViewBag.Response its showing me the raw html. This is not what i want. i want to display the page.

I'm thinking i can do this with an iframe but dont have idea how.

EDIT:

the expected response looks like this:

  <!DOCTYPE html> <html> <head> <link rel='shortcut icon' type='image/x-icon' href='/test_paydirect/favicon.ico' /> <meta name="viewport" content="width=device-width, initial-scale = 1, maximum-scale=1, user-scalable=no" /> <title>WebPAY</title> <link href="/test_paydirect/Content/webpay?v=CSrAVoCR_RCkIRW_2W4qWDLY74gqncHw6gTHkYQDqPI1" rel="stylesheet"/> <script src="/test_paydirect/scripts/jquery?v=YDYC4uCmpbLIjqOyVNC_2sd9YbHnRonWjUni8iH6_Xo1"></script> </head> <body> <div id="outer-frame"> <div id="page-header"> <div id="account-header"> </div> </div> <!--page logo header starts--> <div id="page-logo-header" class=""> <div id="page

Upvotes: 2

Views: 1764

Answers (1)

anthr
anthr

Reputation: 719

Have you tried this in your view?:

    @Html.Raw(ViewBag.Response)

By default the string will be HTML encoded to prevent script injection.

Upvotes: 3

Related Questions