只针对某一个Action不使用Formatter,JSON和XML都不要,然后可以自定义返回内容的Content-Type,而不是根据accept去判断就像MVC里面直接return Content("内容内容","application/xxx");
能不能做到呢?web apimvc

解决方案 »

  1.   

    可以啊,返回string就可以啊:
    public string GetArbitraryString()
    {
        Response.ContentType = "text/plain";
        return "test";
    }
      

  2.   

    在web api的ApiController里面似乎是没有Response这个变量,只有Request用HttpContext.Current.Response这么写是不好使的
      

  3.   

    嗯,我搞错了,应该这样: public HttpResponseMessage Get()
    {
    string result = "test";
    var resp = new HttpResponseMessage(HttpStatusCode.OK);
    resp.Content = new StringContent(result, Encoding.UTF8, "text/plain");
    return resp;
    }
      

  4.   

    赞哦!
    web api接触不久,感觉有很多知识盲点。
    要说用也能用,一往深里去总觉得会遇着问题。
    看来是需要系统地学习,前辈是看书还是看MSDN掌握得这么纯熟呢?
      

  5.   


    我也google了半天。。看来利用搜索引擎的能力急需加强- -
    今天这个我搜的是
    web api contentresult
    web api contenttype
    web api custom contenttype都不好使
      

  6.   

    google: how to return plain string from mvc webapi
    第一条