本文整理汇总了C#中HttpControllerDescriptor类的典型用法代码示例。如果您正苦于以下问题:C# HttpControllerDescriptor类的具体用法?C# HttpControllerDescriptor怎么用?C# HttpControllerDescriptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpControllerDescriptor类属于命名空间,在下文中一共展示了HttpControllerDescriptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldExploreController_MatchesRouteConstraint
public void ShouldExploreController_MatchesRouteConstraint(bool match)
{
// Arrange
var config = new HttpConfiguration();
var ctrlDesc = new HttpControllerDescriptor(config, "sample", typeof(SampleController));
var explorer = new MobileAppApiExplorer(config);
var constraintMock = new Mock<IHttpRouteConstraint>();
var constraint = constraintMock.Object;
var routeMock = new Mock<IHttpRoute>();
routeMock.Setup(r => r.Constraints);
var route = routeMock.Object;
constraintMock.Setup(c => c.Match(null, route, "controller", It.IsAny<IDictionary<string, object>>(), HttpRouteDirection.UriResolution))
.Returns(match)
.Callback<HttpRequestMessage, IHttpRoute, string, IDictionary<string, object>, HttpRouteDirection>((req, rt, p, cnts, rd) =>
{
Assert.Equal("sample", cnts["controller"]);
});
routeMock.Setup(r => r.Constraints)
.Returns(new Dictionary<string, object> { { "controller", constraint } });
// Act
bool actual = explorer.ShouldExploreController("sample", ctrlDesc, route);
// Assert
Assert.Equal(match, actual);
}
示例2: Setting_CustomActionSelector
public void Setting_CustomActionSelector()
{
// Arrange
ApiController api = new UsersController();
HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(UsersController));
controllerContext.ControllerDescriptor = controllerDescriptor;
Mock<IHttpActionSelector> mockSelector = new Mock<IHttpActionSelector>();
mockSelector
.Setup(invoker => invoker.SelectAction(It.IsAny<HttpControllerContext>()))
.Returns(() =>
{
Func<HttpResponseMessage> testDelegate =
() => new HttpResponseMessage { Content = new StringContent("This is a test") };
return new ReflectedHttpActionDescriptor
{
Configuration = controllerContext.Configuration,
ControllerDescriptor = controllerDescriptor,
MethodInfo = testDelegate.Method
};
});
controllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), mockSelector.Object);
// Act
HttpResponseMessage message = api.ExecuteAsync(
controllerContext,
CancellationToken.None).Result;
// Assert
Assert.Equal("This is a test", message.Content.ReadAsStringAsync().Result);
}
示例3: Initialize
public virtual void Initialize(HttpControllerSettings settings, HttpControllerDescriptor descriptor) {
var toRemove = settings.Formatters.Where(t => t is JsonMediaTypeFormatter || t is XmlMediaTypeFormatter).ToList();
foreach (var r in toRemove) {
settings.Formatters.Remove(r);
}
settings.Formatters.Add(new SkybrudJsonMediaTypeFormatter());
}
示例4: GetDescription
public static DataControllerDescription GetDescription(HttpControllerDescriptor controllerDescriptor)
{
return _descriptionMap.GetOrAdd(controllerDescriptor.ControllerType, type =>
{
return CreateDescription(controllerDescriptor);
});
}
示例5: SelectAction_With_DifferentExecutionContexts
public void SelectAction_With_DifferentExecutionContexts()
{
ApiControllerActionSelector actionSelector = new ApiControllerActionSelector();
HttpControllerContext GetContext = ContextUtil.CreateControllerContext();
HttpControllerDescriptor usersControllerDescriptor = new HttpControllerDescriptor(GetContext.Configuration, "Users", typeof(UsersController));
usersControllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), actionSelector);
GetContext.ControllerDescriptor = usersControllerDescriptor;
GetContext.Request = new HttpRequestMessage
{
Method = HttpMethod.Get
};
HttpControllerContext PostContext = ContextUtil.CreateControllerContext();
usersControllerDescriptor.Configuration.Services.Replace(typeof(IHttpActionSelector), actionSelector);
PostContext.ControllerDescriptor = usersControllerDescriptor;
PostContext.Request = new HttpRequestMessage
{
Method = HttpMethod.Post
};
HttpActionDescriptor getActionDescriptor = actionSelector.SelectAction(GetContext);
HttpActionDescriptor postActionDescriptor = actionSelector.SelectAction(PostContext);
Assert.Equal("Get", getActionDescriptor.ActionName);
Assert.Equal("Post", postActionDescriptor.ActionName);
}
示例6: MultipleRoutesForSameMethodException
IReadOnlyList<RouteEntry> IDirectRouteProvider.GetDirectRoutes(HttpControllerDescriptor controllerDescriptor, IReadOnlyList<HttpActionDescriptor> actionDescriptors,
IInlineConstraintResolver constraintResolver)
{
var routes = _provider.GetDirectRoutes(controllerDescriptor, actionDescriptors, constraintResolver);
var list = new List<RouteEntry>();
foreach (var route in routes)
{
var newRoute = new RouteEntry(route.Name ?? Guid.NewGuid().ToString(), route.Route);
list.Add(newRoute);
var descs = route.Route.GetTargetActionDescriptors();
if (descs.Length == 0)
{
continue;
}
foreach (var desc in descs)
{
var reflDesc = desc as ReflectedHttpActionDescriptor;
if (reflDesc == null)
{
continue;
}
var method = reflDesc.MethodInfo;
RouteEntry prevEntry;
if (_map.TryGetValue(method, out prevEntry))
{
throw new MultipleRoutesForSameMethodException(reflDesc, prevEntry, newRoute);
}
_map.Add(method, newRoute);
}
}
return list;
}
示例7: Initialize
/// <summary>
/// Callback invoked to set per-controller overrides for this controllerDescriptor.
/// </summary>
/// <param name="controllerSettings">The controller settings to initialize.</param>
/// <param name="controllerDescriptor">The controller descriptor. Note that the <see
/// cref="T:System.Web.Http.Controllers.HttpControllerDescriptor" /> can be associated with the derived
/// controller type given that <see cref="T:System.Web.Http.Controllers.IControllerConfiguration" /> is
/// inherited.</param>
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
if (controllerSettings == null)
{
throw new ArgumentNullException("controllerSettings");
}
if (controllerDescriptor == null)
{
throw new ArgumentNullException("controllerDescriptor");
}
ServicesContainer services = controllerSettings.Services;
Contract.Assert(services != null);
IContainerMetadata containerMetadata = controllerDescriptor.GetContainerMetadata();
// Replace the action selector with one that is based on the OData routing conventions
IHttpActionSelector originalActionSelector = services.GetActionSelector();
IHttpActionSelector actionSelector;
if (containerMetadata != null)
{
// ContainerMetadata was stored with the HttpControllerDescriptor - so use our "special" ActionSelector
actionSelector = new EntityRepositoryActionSelector(containerMetadata, originalActionSelector);
}
else
{
// No ContainerMetadata stored with the HttpControllerDescriptor - so use the standard odata ActionSelector
actionSelector = new ODataActionSelector(originalActionSelector);
}
controllerSettings.Services.Replace(typeof(IHttpActionSelector), actionSelector);
}
示例8: SelectController
/// <summary>
/// This method is called by Web API system to select the controller for this request.
/// </summary>
/// <param name="request">Request object</param>
/// <returns>The controller to be used</returns>
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
if (request != null)
{
var routeData = request.GetRouteData();
if (routeData != null)
{
string serviceName;
if (routeData.Values.TryGetValue("serviceName", out serviceName))
{
string areaName;
if (routeData.Values.TryGetValue("areaName", out areaName))
{
var controllerName = areaName.ToPascalCase() + "/" + serviceName.ToPascalCase();
var controllerInfo = DynamicApiControllerManager.Find(controllerName);
if (controllerInfo != null)
{
var controllerDescriptor = new HttpControllerDescriptor(_configuration, controllerInfo.Name, controllerInfo.Type);
controllerDescriptor.Properties["__AbpDynamicApiControllerInfo"] = controllerInfo;
return controllerDescriptor;
}
}
}
}
}
return base.SelectController(request);
}
示例9: HttpControllerTracer
IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
IHttpController controller = null;
_traceWriter.TraceBeginEnd(
request,
TraceCategories.ControllersCategory,
TraceLevel.Info,
_innerActivator.GetType().Name,
CreateMethodName,
beginTrace: null,
execute: () =>
{
controller = _innerActivator.Create(request, controllerDescriptor, controllerType);
},
endTrace: (tr) =>
{
tr.Message = controller == null ? SRResources.TraceNoneObjectMessage : controller.GetType().FullName;
},
errorTrace: null);
if (controller != null && !(controller is HttpControllerTracer))
{
controller = new HttpControllerTracer(request, controller, _traceWriter);
}
return controller;
}
示例10: SelectAction_WithDirectRoutes_RespectsRouteOrder
public void SelectAction_WithDirectRoutes_RespectsRouteOrder()
{
// Arrange
var actionSelector = new ApiControllerActionSelector();
HttpControllerContext context = ContextUtil.CreateControllerContext();
context.Request = new HttpRequestMessage { Method = HttpMethod.Get };
var controllerDescriptor = new HttpControllerDescriptor(context.Configuration, "MultipleGet", typeof(MultipleGetController));
context.ControllerDescriptor = controllerDescriptor;
ReflectedHttpActionDescriptor firstDirectRouteAction = (ReflectedHttpActionDescriptor)actionSelector.GetActionMapping(controllerDescriptor)["GetA"].Single();
HttpRouteData[] subRouteData = new HttpRouteData[2];
subRouteData[0] = new HttpRouteData(new HttpRoute());
subRouteData[1] = new HttpRouteData(new HttpRoute());
context.RouteData.Values.Add(RouteCollectionRoute.SubRouteDataKey, subRouteData);
subRouteData[0].Route.DataTokens.Add("actions", new ReflectedHttpActionDescriptor[] { firstDirectRouteAction });
subRouteData[0].Route.DataTokens.Add("order", 1);
ReflectedHttpActionDescriptor secondDirectRouteAction = (ReflectedHttpActionDescriptor)actionSelector.GetActionMapping(controllerDescriptor)["GetB"].Single();
subRouteData[1].Route.DataTokens.Add("actions", new ReflectedHttpActionDescriptor[] { secondDirectRouteAction });
subRouteData[1].Route.DataTokens.Add("order", 2);
// Act
HttpActionDescriptor actionDescriptor = actionSelector.SelectAction(context);
// Assert
Assert.Same(secondDirectRouteAction, actionDescriptor);
}
示例11: GetActionMapping
public override ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor)
{
if(controllerDescriptor.ControllerType == typeof(SlimApiGhostController)) {
var newActions = new List<HttpActionDescriptor>();
foreach(var contrInfo in _apiConfig.ControllerInfos) {
var methods = contrInfo.Type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
foreach(var method in methods) {
var dtype = method.DeclaringType;
if (dtype == typeof(object)) //skip ToString()
continue;
var action = new SlimApiActionDescriptor(controllerDescriptor, method, contrInfo, _apiConfig);
if (action.RouteTemplates.Count > 0 && action.SupportedHttpMethods.Count > 0) {
RegisterAction(action);
newActions.Add(action);
}
}
} //foreach ct
var lkp = newActions.ToLookup(a => a.ActionName, StringComparer.OrdinalIgnoreCase);
return lkp;
}
// otherwise call base
return base.GetActionMapping(controllerDescriptor);
}
示例12: HttpControllerContext
/// <summary>Initializes a new instance of the <see cref="HttpControllerContext"/> class.</summary>
/// <param name="requestContext">The request context.</param>
/// <param name="request">The HTTP request.</param>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <param name="controller">The controller.</param>
public HttpControllerContext(HttpRequestContext requestContext, HttpRequestMessage request,
HttpControllerDescriptor controllerDescriptor, IHttpController controller)
{
if (requestContext == null)
{
throw Error.ArgumentNull("requestContext");
}
if (request == null)
{
throw Error.ArgumentNull("request");
}
if (controllerDescriptor == null)
{
throw Error.ArgumentNull("controllerDescriptor");
}
if (controller == null)
{
throw Error.ArgumentNull("controller");
}
_requestContext = requestContext;
_request = request;
_controllerDescriptor = controllerDescriptor;
_controller = controller;
}
示例13: GetDocumentation
/// <summary>
/// Gets the documentation based on <see cref="System.Web.Http.Controllers.HttpControllerDescriptor"/>.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <returns>The documentation for the controller.</returns>
public string GetDocumentation(HttpControllerDescriptor controllerDescriptor)
{
return this.sources
.Select(source => source.GetDocumentation(controllerDescriptor))
.Where(documentation => !string.IsNullOrEmpty(documentation))
.FirstOrDefault();
}
示例14: Initialize
public void Initialize(HttpControllerSettings controllerSettings,
HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.ParameterBindingRules.Insert(0,
new Func<HttpParameterDescriptor, HttpParameterBinding>(
d => new SimplePostVariableParameterBinding(d)));
}
示例15: Create
public IHttpController Create(HttpRequestMessage request
, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = _container.GetInstance(controllerType) as IHttpController;
return controller;
}