我想使用自定义操作过滤器来操作一个操作的参数。
用户输入:表格中的 2 个姓名;
Action:实际上需要取2个id;
Action Filter(onExecuting,将验证输入名称,如果有效,将它们转换为 2 个 id 并替换为路由数据),
因为我不想将验证逻辑放入 Action Controller 中。
这是代码的一部分:
-
路由信息
routes.MapRoute(
"默认", // 路由名称
"{controller}/{action}", // 带参数的 URL
new {controller = "Home", action = "Index"} // 参数默认值
);
路线.MapRoute(
"RelationshipResults", // 路由名称
"Relationship/{initPersonID}/{targetPersonID}", // 带参数的 URL
新{控制器=“关系”,操作=“结果”}
);
要提交的表单(创建2个输入框并通过jquery提交)
...
<%: MvcWeibookWeb.Properties.Resource.Home_InitPersonName%>;
<%= Html.TextBox("initPersonID")%>
<%: MvcWeibookWeb.Properties.Resource.Home_SinaIntro %>
>
<%: MvcWeibookWeb.Properties.Resource.Genearl_PromotionSina %>
<%:MvcWeibookWeb.Properties.Resource.Home_TargetPersonName%>
<%= Html.TextBox("targetPersonID")%>
<%: MvcWeibookWeb.Properties.Resource.Home_Search%>
-
操作过滤器
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Sina.Searcher 搜索器 = new Sina.Searcher(Sina.Processor.UserNetwork);
字符串 initPersonName, targetPersonName;
// 表单提交名称,我们需要在进入真正的控制器之前对其进行处理并将其转换为ID。
initPersonName = filterContext.RouteData.Values["initPersonID"] as String;
targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;
// 做一些事情将其转换为 ids 并替换
动作/控制器
<前><代码> [ValidationActionFilter]
[处理错误]
公共 ActionResult 结果(Int64 initPersonID,Int64 targetPersonID)
{ ...
我的问题是:在actionFilter中,它永远不会获取2个参数“initPersonID”和“targetPersonID”,RouteData.Values不包含这2个键。
I want to use custom action filter to manipulate parameters to one action.
User inputs: 2 names in a form ;
Action: actually needs to take 2 ids;
Action Filter (onExecuting, will verify the input names and if valid, convert them into 2 ids and replace in the routedata)
because i don't want to put validation logic in Action Controller.
here's part of the code:
-
Routing Info
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter default
);
routes.MapRoute(
"RelationshipResults", // Route Name
"Relationship/{initPersonID}/{targetPersonID}", // URL with parameters
new { controller = "Relationship", action = "Results" }
);
-
Form to submit (Create 2 input box and submit via jquery)
...
<td align="left"><%: MvcWeibookWeb.Properties.Resource.Home_InitPersonName%></td>
<td align="right"> <%= Html.TextBox("initPersonID")%></td>
<td rowspan="3" valign="top">
<div id="sinaIntro">
<%: MvcWeibookWeb.Properties.Resource.Home_SinaIntro %>
<br />
<%: MvcWeibookWeb.Properties.Resource.Genearl_PromotionSina %>
</div>
</td>
</tr>
<tr>
<td align="left" width="90px"><%: MvcWeibookWeb.Properties.Resource.Home_TargetPersonName%></td>
<td align="right"><%= Html.TextBox("targetPersonID")%></td>
</tr>
<tr>
<td colspan="2" align="right">
<a href="#" class="btn-HomeSearch" onclick="$('#formSearch').submit();"><%: MvcWeibookWeb.Properties.Resource.Home_Search%></a>
</td>
-
Action Filter
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Sina.Searcher searcher = new Sina.Searcher(Sina.Processor.UserNetwork);
String initPersonName, targetPersonName;
// form submit names, we need to process them and convert them to IDs before it enters the real controller.
initPersonName = filterContext.RouteData.Values["initPersonID"] as String;
targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;
// do sth to convert it to ids and replace
-
Action/Controller
[ValidationActionFilter]
[HandleError]
public ActionResult Results( Int64 initPersonID, Int64 targetPersonID)
{ ...
My problem is: in the actionFilter, it never gets the 2 parameters "initPersonID" and "targetPersonID", the RouteData.Values don't contain these 2 keys.
发布评论
评论(1)
问题是,由于您的路线没有任何
initPersonName
和targetPersonName
值,因此它们永远不会出现在您的路线数据中。尝试(尽管看起来有点奇怪):由于“...ID”是您的路线中调用的值,因此您必须在路线数据中查找这些值。事实上,你实际上没有在 url 中包含 ID 是另一回事......
The problem is that since your routes don't have any values for
initPersonName
andtargetPersonName
, they never end up in your route data. Try (even though it looks a little odd):Since "...ID" whas what the values where called in your routes, that's what you have to look for in your route data. The fact that you're actually not including the ID's in the url is another matter...