是否可以使用 ServiceStack 提供 HTML 页面? [英] Is it possible to serve HTML pages with ServiceStack?

查看:45
本文介绍了是否可以使用 ServiceStack 提供 HTML 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估在 Windows 服务中使用 ServiceStack 来托管 REST 服务.到目前为止,它非常出色,并且表现优于 WCF.更不用说它更易于使用.

I'm evaluating ServiceStack for use in a Windows Service to host REST services. So far, it's excellent and way outperforms WCF. Not to mention its much easier to use.

上面提到的 Windows 服务需要能够提供一个简单的 HTML 页面来充当仪表板".ServiceStack当然会被用来为仪表板提供json数据,但是如何为仪表板页面提供图像和js呢?这可能吗?我宁愿不托管成熟的 ASP.Net 并且有一个严格的限制,即 IIS 永远不会在这些服务器上运行.也不应该要求可扩展性,因为只有一台管理机器会显示仪表板.

The Windows Services mentioned above need to be able to provide a simple HTML page to serve as a "dashboard". ServiceStack would of course be used to provide json data for the dashboard, but what about serving the dashboard page, along with images and js? Would this be possible? I would rather not host full-blown ASP.Net and have a strict limitation that IIS will never run on these servers. Scalability shouldnt be a requirement either, since only a single admin machine would ever be displaying the dashboard.

推荐答案

仅将 ServiceStack 用于 Web 和 Web 服务

ServiceStack 的新 Razor 视图引擎支持

v3.9.11ServiceStack.Razor NuGet 包.有了这种支持,ServiceStack 现在可以升级为完整的网站和 Web 服务框架,该框架可以更简单地替代 WCF、MVC 和 WebApi.

Using only ServiceStack for web and web services

ServiceStack's new Razor View Engine support

A significant improvement to ServiceStack's HTML story was added in v3.9.11 in the ServiceStack.Razor NuGet package. With this support ServiceStack now graduates to a full Website and Web Services framework that offers a much simpler replacement to WCF, MVC and WebApi.

Razor Rockstars 演示网站中维护了解释 ServiceStack 与 Razor 支持的新 HTML 故事的完整文档:

Full documentation explaining ServiceStack's new HTML story with Razor Support is maintained in the Razor Rockstars demo website:

  • http://razor.servicestack.net - ASP.NET Host developed on Win hosted on Linux
  • http://razor-console.servicestack.net - Stand-alone Self Host developed on Win hosted on Linux

在上面的链接中解释了解释 Razor 支持和描述其功能的完整文档.

Full Documentation explaining Razor support and describing its Features is explained in the links above.

HTML Content-Type 已添加到 ServiceStack 中,正如您对真正的 REST 服务框架所期望的那样,即您可以简单地添加 Razor 视图来增强您现有的服务,这些服务只会在客户端请求 text/html 内容类型(对现有的注册格式没有影响).例如.这个 /rockstars REST 服务仍然可以在所有其他内容类型中访问:

The HTML Content-Type has been added to ServiceStack just as you would expect from a true REST Service framework, i.e. you can simply add Razor Views to enhance your existing services which will only get used when the client requests for the text/html Content-Type (with no effect on the existing registered formats). E.g. this /rockstars REST service can still be accessed in all the other Content-Types:

此外,如果您的服务具有 [ClientCanSwapTemplates] 属性,则客户端可以在运行时交换页面的视图和模板,例如这是与上面相同的页面:

In addition if your services has the [ClientCanSwapTemplates] attribute, the client can swap the Views and Templates of pages at runtime, e.g. here's the same above page with:

ServiceStack 以这种方式自然地采用 HTML 格式,这使得开发一组可以同时为 HTML 和丰富的原生移动和桌面客户端提供服务的服务变得微不足道.

ServiceStack's natural adoption of the HTML format in this way, makes it trivial to develop 1 set of services that can serve both HTML and rich native mobile and desktop clients.

在添加 Razor 支持之前,有几种使用 ServiceStack 提供 HTML 页面的策略:

Before Razor support was added there are a couple of strategies of serving HTML pages with ServiceStack:

如果您对现有文件发出 Web 请求,它会与静态文件处理程序一起返回.然后,您可以简单地将 ajax json 调用回您的 Web 服务以动态生成页面.

If you make a web request for an existing file, it gets returned with the Static File Handler. You can then simply make ajax json calls back to your web services to dynamically generate a page.

Windows 服务 AppHost 启动模板中的 TODO Backbone 应用程序 就是这样工作的.(以及 ServiceStack 中的大多数其他示例项目 :-)

The TODO Backbone application in the Windows Service AppHost Starter Template works this way. (as well as most other example projects in ServiceStack :-)

从您的 Web 服务返回的任何 字符串 都将直接写入响应流原样",因此您可以使用自己的 html 模板解决方案简单地返回 html.

Any string returned from your web services gets directly written to the response stream 'as-is', so you can simply return html using your own html templating solution.

这是 ServiceStack 中其他可能的返回类型的列表以及它们是如何治疗.

Here's a list of other possible return types in ServiceStack and how they're treated.

ServiceStack 内置的视图引擎是 Markdown Razor - 这是受 MVC 的 Razor 启发,但使用 Markdown 语法.它非常可扩展,支持自定义基类和扩展方法/实用程序.

The view-engine built into ServiceStack is Markdown Razor - Which was inspired by MVC's Razor but using Markdown syntax. It's quite extensible supporting custom base class and extension methods/utils.

使用 Markdown Razor 的一个很好的特性是,您返回 json、xml 等的相同 Web 服务也可以是同一 url 上动态生成的 html 页面的视图模型.

A nice feature of using Markdown Razor is your same web service that returns json,xml, etc can also be a view model for a dynamically generated html page at the same url.

一个例子是 category web service,你可以在这里看到结果:http://www.servicestack.net/docs/category/Framework

An example of this is the category web service which you can see the results of here: http://www.servicestack.net/docs/category/Framework

JSON 中再次使用相同的服务,XML 等也可以检索部分生成的 html 页面(没有模板)以及 动态生成的降价.

and the same service again in JSON, XML, etc. You can also retrieve the partially generated html page (without the template) as well the dynamically generated markdown.

该页面是使用发送到此 MarkdownRazor 视图的 Web 服务 DTO/视图模型创建的https://raw.github.com/ServiceStack/ServiceStack.Examples/master/src/Docs/Views/Category.md

The page was created using the web services DTO/view model which was sent to this MarkdownRazor View https://raw.github.com/ServiceStack/ServiceStack.Examples/master/src/Docs/Views/Category.md

如果你为你的网络服务指定了一个 Markdown Razor 页面,它会被默认使用 您现在看到的 HTML5 JSON 报告.

If you have specified a Markdown Razor page for your web service, it is used over the default HTML5 JSON Report that you see now.

ServiceStack 用于解析用于呈现 HTML 输出的适当 Markdown 模板的解析顺序是:

The resolution order ServiceStack's uses to resolve the appropriate Markdown template to use for rendering HTML output is:

  • 如果 Web 服务指定了模板(通过自定义的 IHttpResult.TemplateName 响应) - 则具有该名称的视图.
  • 与响应 DTO 同名的视图,首先在/Views 中查找,然后在/Views/Shared 中查找
  • 与请求 DTO 同名的视图,首先在/Views 中查找,然后在/Views/Shared 中查找

ServiceStack 可以与现有的 ASP.NET Web 应用程序一起使用,也可以不使用.如果您的应用程序HTML-heavyREST-Services-light 一个常见的方法是在/custompath(例如/api),以便您可以使用 ASP.NET 生成所有 HTML 页面.

ServiceStack can be used together with or without an existing ASP.NET web application. If your application is HTML-heavy and REST-Services-light a common approach is to host ServiceStack at a /custompath (e.g. /api) so you can use ASP.NET for all HTML page generation.

如果使用 ASP.NET MVC,则需要忽略 MVC 的 Global.asax RegisterRoutes() 中的路由:

If using ASP.NET MVC instead, you need to ignore the route in MVC's Global.asax RegisterRoutes():

routes.IgnoreRoute ("servicestack/{*pathInfo}");

这篇关于是否可以使用 ServiceStack 提供 HTML 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆