結果をデフォルトの XML や JSON ではなく CSS プレーン テキストとして返す Web API メソッドを作成する必要があります。使用する必要がある特定のプロバイダーはありますか?
ContentResult クラス ( http://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult(v=vs.108).aspx ) を使用してみましたが、うまくいきませんでした。
ありがとう
結果をデフォルトの XML や JSON ではなく CSS プレーン テキストとして返す Web API メソッドを作成する必要があります。使用する必要がある特定のプロバイダーはありますか?
ContentResult クラス ( http://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult(v=vs.108).aspx ) を使用してみましたが、うまくいきませんでした。
ありがとう
コンテンツ ネゴシエーションをバイパスする必要があります。つまり、の新しいインスタンスをHttpResponseMessage
直接返し、コンテンツとコンテンツ タイプを自分で設定する必要があります。
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(".hiddenView { display: none; }", Encoding.UTF8, "text/css")
};
そして、楽しみのために積み上げて、.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;
}
ここでの回答をインスピレーションとして使用します。次のような簡単なことを実行できるはずです。
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;
}
AspNet Core WebApi を使用している場合は、次のように簡単に実行できます
[HttpGet("custom.css")]
public IActionResult GetCustomCss()
{
var customCss = ".my-class { color: #fff }";
return Content(customCss, "text/css");
}