德米特里的解决方案让我最的方式,我想在那里,但是routeName构造函数PARAM是一个问题,因为StructureMap不知道要放什么东西在那里。内部hyprlink使用UrlHelper来生成URI,并且想要知道要使用的路由名称
在那一点上,我明白了为什么URI生成非常棘手,因为它与路由配置中的路由名称绑定在一起,并且为了支持POST,我们需要将该方法与正确的routename相关联,并且这在调度器时间是未知的。默认的hyprlinkr假设只有一个名为“DefaultRoute”的路由配置
我改变了Dimitry的代码,并采用了基于约定的方法,其中以“Get”开头的控制器方法映射到名为“Get”的路由,以“Add”开头的控制器方法被映射到名为“Add”的路由。
我不知道是否有更好的方法与正确的命名routeConfig关联的方法?
public class RemoveFromBodyParamsRouteDispatcher : IRouteDispatcher
{
private static readonly ILog _log = LogManager.GetLogger(typeof (RemoveFromBodyParamsRouteDispatcher));
public Rouple Dispatch(MethodCallExpression method,
IDictionary<string, object> routeValues)
{
var methodName = method.Method.Name;
DefaultRouteDispatcher defaultDispatcher;
if (methodName.StartsWith("Get"))
defaultDispatcher = new DefaultRouteDispatcher("Get");
else if (methodName.StartsWith("Add"))
defaultDispatcher = new DefaultRouteDispatcher("Add");
else
throw new Exception("Unable to determine correct route name for method with name " + methodName);
_log.Debug("Dispatch methodName=" + methodName);
//make a copy of routeValues as contract says we should not modify
var routeValuesWithoutFromBody = new Dictionary<string, object>(routeValues);
var routeKeysToRemove = new HashSet<string>();
foreach (var paramName in routeValuesWithoutFromBody.Keys)
{
var parameter = method.Method
.GetParameters()
.FirstOrDefault(p => p.Name == paramName);
if (parameter != null)
if (IsFromBodyParameter(parameter))
{
_log.Debug("Dispatch: Removing paramName=" + paramName);
routeKeysToRemove.Add(paramName);
}
}
foreach (var routeKeyToRemove in routeKeysToRemove)
routeValuesWithoutFromBody.Remove(routeKeyToRemove);
return defaultDispatcher.Dispatch(method, routeValuesWithoutFromBody);
}
private static bool IsFromBodyParameter(ParameterInfo parameter)
{
//Apparently the "inherit" argument is ignored: http://msdn.microsoft.com/en-us/library/cwtf69s6(v=vs.100).aspx
const bool msdnSaysThisArgumentIsIgnored = true;
var attributes = parameter.GetCustomAttributes(msdnSaysThisArgumentIsIgnored);
return attributes.Any(ct => ct is FromBodyAttribute);
}
}
这几乎是逐字我在hyprlinkr维基提出的一个问题:https://github.com/ploeh/Hyprlinkr/issues/28我现在,我没有提高之前检查所以首先我很尴尬。今后,我会先在这里发布我的问题! – 2014-01-27 11:43:24