【发布时间】:2017-07-04 13:55:16
【问题描述】:
- 我的应用程序在多个服务器上运行。
- 我希望其中一台服务器处理特定的 api 请求子集,因为它与缓存交互。可以使用我给它的特殊 CMS Url 联系该服务器:
Config.CMSUrl
我有以下代码:
public enum ServerType
{
CMS
}
public class RunOnServerAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public ServerType Type;
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
{
switch (Type)
{
case ServerType.CMS:
if (!filterContext.Request.RequestUri.AbsoluteUri.StartsWith(Config.CMSUrl))
{
var response = filterContext.Request.CreateResponse(System.Net.HttpStatusCode.Moved);
response.Headers.Location = new Uri($"{Config.CMSUrl}/{filterContext.Request.RequestUri.PathAndQuery}");
}
break;
}
base.OnActionExecuting(filterContext);
}
}
这应该允许开发人员向 API 方法添加一个简单的属性,该属性将准确指示该方法需要运行的位置(因此任何处理与缓存交互的方法都应该通过 cms 服务器定向)。
为了维护身份验证、表单数据和我可能拥有的任何其他请求/响应架构,使用此方法是否安全?还是我可能在这里冒险?
我的另一个选择是确保调用 url 的人首先正确使用 CMS Url 而不是普通的 api url...但我的问题是可能的维护和人为错误。
【问题讨论】: