ASP.Net MVC:根据参数值映射路由
我正在尝试在网站上创建类似向导的工作流程,并且每个步骤都有一个模型。
我有以下操作方法:
public ActionResult Create();
public ActionResult Create01(Model01 m);
public ActionResult Create02(Model02 m);
public ActionResult Create03(Model03 m);
我希望用户将地址视为
/Element/Create
/Element/Create?Step=1
/Element/Create?Step=2
/Element/Create?Step=3
所有模型类都继承自具有 Step 属性的 BaseModel。 具有参数的操作方法具有正确的 AcceptVerbs 约束。
我尝试将所有方法命名为 Create,但这导致了 AmbigeousMatchException。
我现在想做的是为每一项操作创建一个自定义路线,但我不知道如何做到这一点。 这就是我尝试过的:
routes.MapRoute(
"ElementsCreation",
"Element/Create",
new{controller="Element", action="Create01"},
new{Step="1"}
);
但这不起作用。
任何帮助(关于正确的 MapRoute 调用或可能不同的方法)将不胜感激。
谢谢
I'm trying to create a wizard-like workflow on a site, and I have a model for each one of the steps.
I have the following action methods:
public ActionResult Create();
public ActionResult Create01(Model01 m);
public ActionResult Create02(Model02 m);
public ActionResult Create03(Model03 m);
And I want the user to see the address as
/Element/Create
/Element/Create?Step=1
/Element/Create?Step=2
/Element/Create?Step=3
All the model classes inherit from a BaseModel that has a Step property.
The action methods that have the parameters have the correct AcceptVerbs constraint.
I tried naming all the methods Create, but that resulted in a AmbiguousMatchException.
What I want to do now is to create a custom route for each one of the actions, but I can't figure out how to do it.
This is what I tried:
routes.MapRoute(
"ElementsCreation",
"Element/Create",
new{controller="Element", action="Create01"},
new{Step="1"}
);
But this doesn't work.
Any help (on the correct MapRoute call or maybe a different approach) would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上找到了一种不同的方法。
我没有添加新的路线图,而是创建了一个新的操作方法属性来验证传递的请求对于每个操作方法是否有效。
这是属性类:
我的每个操作方法都具有相同的名称,并按如下方式装饰:
我更喜欢这种方法,而不是为每个方法创建一个路由。
I actually found a different approach.
Instead of adding a new Route Map, I created a new Action Method attribute to verify if the passed request is valid for each of the action methods.
This is the attribute class:
And I have each one of the action methods with the same name and decorated like this:
I like this approach a LOT more than creating one route for each method.