ASP.Net MVC:根据参数值映射路由

发布于 2024-10-15 15:48:44 字数 815 浏览 1 评论 0原文

我正在尝试在网站上创建类似向导的工作流程,并且每个步骤都有一个模型。

我有以下操作方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-10-22 15:48:44

我实际上找到了一种不同的方法。

我没有添加新的路线图,而是创建了一个新的操作方法属性来验证传递的请求对于每个操作方法是否有效。

这是属性类:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ParameterValueMatchAttribute : ActionMethodSelectorAttribute
{
   public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
   {
      var value = controllerContext.RequestContext.HttpContext.Request[Name];
      return (value == Value);
   }

   public string Value { get; set; }
   public string Name { get; set; }
}

我的每个操作方法都具有相同的名称,并按如下方式装饰:

[AcceptVerbs(HttpVerbs.Post)]
[ParameterValueMatch(Name="Step", Value="1")]
public ActionResult Create(Model01 model)

我更喜欢这种方法,而不是为每个方法创建一个路由。

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:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ParameterValueMatchAttribute : ActionMethodSelectorAttribute
{
   public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
   {
      var value = controllerContext.RequestContext.HttpContext.Request[Name];
      return (value == Value);
   }

   public string Value { get; set; }
   public string Name { get; set; }
}

And I have each one of the action methods with the same name and decorated like this:

[AcceptVerbs(HttpVerbs.Post)]
[ParameterValueMatch(Name="Step", Value="1")]
public ActionResult Create(Model01 model)

I like this approach a LOT more than creating one route for each method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文