简体   繁体   English

使用OWIN自托管输出HTML

[英]Outputting html with OWIN selfhost

I'm experimenting with OWIN selfhost, and I'm trying to output a html response, however the server is always returning xml output. 我正在尝试OWIN自托管,并且正在尝试输出html响应,但是服务器始终返回xml输出。 What am I doing wrong? 我究竟做错了什么?

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            "default", 
            "{controller}/{action}/{id}", 
            new { controller = "home", action = "index", id = RouteParameter.Optional }
            );

        app.UseWebApi(config);
    }
}

public class HomeController : ApiController
{
    public HomeController()
    {
    }

    [HttpGet]
    public string Index()
    {
        return "<b>kg</b>";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var options = new StartOptions();
        options.Urls.Add("http://+:8181");

        using (WebApp.Start<Startup>(options))
        {
            Console.WriteLine("Server started");

            Console.ReadLine();
        }
    }
}

You're using WebApi (ApiController). 您正在使用WebApi(ApiController)。 This is for producing xml or JSON, not html. 这是用于生成xml或JSON,而不是html。 MVC controllers that produce HTML do not work in selfhost until you move to ASP.NET Core. 除非您移至ASP.NET Core,否则产生HTML的MVC控制器无法在自托管中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM