0

I am creating an asp.net web service and I want to use an HTML page with son content to be passed to it. I have the json data in the controller but when I return, I want to specify which view to sealing with the json data.

[HttpPost]
public async Task<IActionResult> Create(IFormFile postedFile)
{
    byte[] data;
    using (var br = new BinaryReader(postedFile.OpenReadStream()))
    {
        data = br.ReadBytes((int)postedFile.OpenReadStream().Length);
    }

    HttpContent fileContent = new ByteArrayContent(data);
    string uirWebAPI = _configuration.GetValue<string>("Api");

    using (var client = new HttpClient())
    {
        using (var formData = new MultipartFormDataContent())
        {
            formData.Add(fileContent, "file", "File");
            client.DefaultRequestHeaders.Add("blobPath", meshFilePath);
            // calling another API to do some processing and return a json response
            var response = client.PostAsync(uirWebAPI, formData).Result;
            if (response.IsSuccessStatusCode)
            {
                using (Stream responseStream = await response.Content.ReadAsStreamAsync())
                {
                    jsonMessage = new StreamReader(responseStream).ReadToEnd();
                }
                var jsonString = await response.Content.ReadAsStringAsync();
                jsonObject = JsonConvert.DeserializeObject<object>(jsonString);
            }
            else
            {
                return null;
            }
        }
    }

    ViewData["jsonData"] = jsonString;
    return View();
}

I want to do something like:

var jsonData = jsonObject 
return View("myHTMLpage", jsonData);

How to do it with ASP.NET MVC ?

yogihosting
  • 5,494
  • 8
  • 47
  • 80
  • 1
    Create a model that has the structure of what you want. Set the model to the result of `returnFromAPI`. Pass model into the view. – JamesS May 12 '20 at 08:52
  • I tried this and it worked for me, thanks. However, I want to use my HTML page instead of csHTML for this, is there a way for that. I found a way using csHTML and setting layout to null but I want to use HTML files only. – himalayansailor May 15 '20 at 08:15

1 Answers1

2

You can create custom ActionResult as JsonNetResult

public class JsonNetResult : ActionResult
{
    public Encoding ContentEncoding { get; set; }
    public string ContentType { get; set; }
    public object Data { get; set; }

    public JsonSerializerSettings SerializerSettings { get; set; }
    public Formatting Formatting { get; set; }

    public JsonNetResult() {
        SerializerSettings = new JsonSerializerSettings();
    }

    public override void ExecuteResult( ControllerContext context ) {
        if ( context == null )
            throw new ArgumentNullException( "context" );

        HttpResponseBase response = context.HttpContext.Response;

        response.ContentType = !string.IsNullOrEmpty( ContentType )
            ? ContentType
            : "application/json";

        if ( ContentEncoding != null )
            response.ContentEncoding = ContentEncoding;

        if ( Data != null ) {
            JsonTextWriter writer = new JsonTextWriter( response.Output ) { Formatting = Formatting };

            JsonSerializer serializer = JsonSerializer.Create( SerializerSettings );
            serializer.Serialize( writer, Data );

            writer.Flush();
        }
    }
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62