3

結果をデフォルトの XML や JSON ではなく CSS プレーン テキストとして返す Web API メソッドを作成する必要があります。使用する必要がある特定のプロバイダーはありますか?

ContentResult クラス ( http://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult(v=vs.108).aspx ) を使用してみましたが、うまくいきませんでした。

ありがとう

4

5 に答える 5

6

コンテンツ ネゴシエーションをバイパスする必要があります。つまり、の新しいインスタンスをHttpResponseMessage直接返し、コンテンツとコンテンツ タイプを自分で設定する必要があります。

return new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(".hiddenView { display: none; }", Encoding.UTF8, "text/css")
    };
于 2013-07-02T09:24:32.830 に答える
0

そして、楽しみのために積み上げて、.cssをコントローラーと同じフォルダーにある埋め込みファイルとして保存すると仮定して、セルフホストでも機能するバージョンを次に示します。すべてのVSインテリセンスを取得できるため、ソリューション内のファイルに保存すると便利です。また、このリソースはあまり変更されない可能性があるため、キャッシュを少し追加しました。

  public HttpResponseMessage Get(int id)
    {

        var stream = GetType().Assembly.GetManifestResourceStream(GetType(),"site.css");

        var cacheControlHeader = new CacheControlHeaderValue { MaxAge= new TimeSpan(1,0,0)};

        var response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK, 
                CacheControl = cacheControlHeader,
                Content = new StreamContent(stream, Encoding.UTF8, "text/css" )
            };

        return response;
    }
于 2013-07-02T14:10:43.023 に答える
0

ここでの回答をインスピレーションとして使用します。次のような簡単なことを実行できるはずです。

public HttpResponseMessage Get()
{
    string css = @"h1.basic {font-size: 1.3em;padding: 5px;color: #abcdef;background: #123456;border-bottom: 3px solid #123456;margin: 0 0 4px 0;text-align: center;}";
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(css, Encoding.UTF8, "text/css");
    return response;
}
于 2013-07-02T09:31:01.653 に答える
0

AspNet Core WebApi を使用している場合は、次のように簡単に実行できます

 [HttpGet("custom.css")]
 public IActionResult GetCustomCss()
 {
     var customCss = ".my-class { color: #fff }";

     return Content(customCss, "text/css");
 }
于 2020-12-29T01:31:16.233 に答える