从 Razor 调用 ServiceStack 服务 [英] Calling a ServiceStack service from Razor

查看:27
本文介绍了从 Razor 调用 ServiceStack 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有点边缘情况:我需要从 razor(同一个网站)调用 servicestack 服务现在我在做

A bit of an edge case here: I need to call a servicestack service from razor (same website) Right now I'm doing

CheckIfConfiguredResponse aResponse= new JsonServiceClient("http:\\localhost:2000").Get<CheckIfConfiguredResponse>("/CheckIfConfigured"); 

这是正确的做法吗?或者有更好的吗?另外,我如何避免手动指定网址并让它自动填充主机(因为它是同一个网站)

Is that the proper way to go about doing it? Or is there better? Also, How do I eliminate having to specify the web address manually and have it automatically populate the host (since it's the same web site)

提前致谢,会.

推荐答案

您永远不想为了调用 ServiceStack 服务而对自己进行 HTTP 回调.

You never want to make a HTTP call back to yourself just to call a ServiceStack service.

与其他框架不同,ServiceStack 中的服务只是自动连接的 C# 类型,您可以像其他已注册的 IOC 依赖项一样从 IOC 访问它们.即在 Razor 视图中,您可以简单地解析它并直接从 IOC 调用它:

Unlike other frameworks, Services in ServiceStack are simply auto-wired C# types which you can access from the IOC like every other registered IOC dependency. i.e. Inside a Razor View you can simply resolve it and call it directly from the IOC with:

var response = base.Get<CheckIfConfiguredService>().Get(new CheckIfConfigured());

这会像普通的自动连接的 C# 依赖项一样解析和调用服务,但不会注入当前的请求上下文.如果您的服务确实需要它,您可以改为使用 AppHostBase.ResolveService 这样做,例如:

This resolves and calls the service like a normal auto-wired C# dependency, but doesn't inject the current request context. If your service does need it, you can instead use AppHostBase.ResolveService which does, e.g:

var response = AppHostBase
  .ResolveService<CheckIfConfiguredService>(HttpContext.Current)
  .Get(new CheckIfConfigured());

这篇关于从 Razor 调用 ServiceStack 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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