Base solution for your next web application
Open Closed

Dynamic WebApi Response ContentType #2514


User avatar
0
aplanos created

Hi, thanks for this great framework. How can I change the dynamic web api response, for example, by default is application/json, sometimes I need it to be text/html. Is possible?


2 Answer(s)
  • User Avatar
    0
    rbarbosa created

    I ran into this a while ago, this is a webAPI issue, check this code as an example for the service

    public interface IHtmlResponseAppService : IApplicationService
     {
         HttpResponseMessage GetHTMLResult()
     } 
    
     public class HtmlResponseAppService : AbpAppServiceBase, IHtmlResponseAppService
     {
        public HttpResponseMessage GetHTMLResult()
        {
            string result = "something";
    
            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/html");
            return resp;
        }
    }
    

    works on dynamic webapi and whatnot

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I haven't tried this but it should work like this. Try adding [DontWrapResult] attribute if it does not work for the first time.

    var path = "your path to index.html";
    var response = new HttpResponseMessage();
    response.Content =  new StringContent(File.ReadAllText(path));
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;