返回HTML字符串
返回带有媒体类型text / html的字符串内容:
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
response.Content = new StringContent("
Hello World");response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
从Web API返回Razor视图
您需要使用NuGet控制台安装软件包RazorEngine。
using RazorEngine;
using RazorEngine.Templating;
string razorTemplate = "Hello @Model!";
string html = Engine.Razor.RunCompile(
razorTemplate,
"uniqueTemplateKey",
modelType: typeof(string),
model: "Lorem Ipsum");
现在我们可以使用前面例子中的代码从Web API返回HTML字符串。