forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuisActionModelBinder.cs
45 lines (39 loc) · 1.37 KB
/
LuisActionModelBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace LuisActions.Samples.Web
{
using System;
using System.Web;
using System.Web.Mvc;
using Microsoft.Cognitive.LUIS.ActionBinding;
public class LuisActionModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext context)
{
HttpRequestBase request = controllerContext.HttpContext.Request;
var type = request.Form["LuisActionType"];
if (!string.IsNullOrWhiteSpace(type))
{
var action = Activator.CreateInstance(Type.GetType(type));
Func<object> modelAccessor = () => action;
context.ModelMetadata = new ModelMetadata(
new DataAnnotationsModelMetadataProvider(),
context.ModelMetadata.ContainerType,
modelAccessor,
action.GetType(),
context.ModelName);
return base.BindModel(controllerContext, context);
}
return null;
}
}
public class LuisActionModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(Type modelType)
{
if (modelType == typeof(ILuisAction))
{
return new LuisActionModelBinder();
}
return null;
}
}
}