ASP.NET MVC 中到不同默认操作的多个默认路由

发布于 2024-09-03 07:36:07 字数 980 浏览 2 评论 0原文

我有多个具有不同操作的控制器(没有“索引”操作)。我认为“默认”操作的操作名称不同。 我想为其名称创建默认路由,并且如果路由中仅提供了控制器名称,则执行第一个可用操作(来自我的默认操作列表)。

因此,例如,我有以下操作,我想考虑默认,并希望按以下顺序检查它们在控制器中是否存在:

List() 
Draw()
ViewSingle() 

路由应该以某种方式搜索 /{controller} ,然后采用上面列表中的第一个可用操作作为默认操作,例如:

/ControllerA     -> ControllerA.List()
/ControllerB     -> ControllerB.Draw()
/ControllerC     -> ControllerC.ViewSingle()
/ControllerD     -> ControllerD.Draw()
/ControllerE     -> ControllerE.List()

这可能吗?我尝试创建像这样的其他默认操作,但无法使其工作:

routes.MapRoute("Default1", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "List" } 

routes.MapRoute("Default2", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "Draw" } 

routes.MapRoute("Default3", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "ViewSingle" } 

帮助?

I have multiple controllers with different actions (no "Index" actions). The actions I consider "default" actions, are named differently. I want to create default routes for their names and have the first available action (from my list of default actions) executed if only the controller name is provided in the route.

So, for example, I have the following actions which I want to consider default and want checked for their existence in a controller in this order:

List() 
Draw()
ViewSingle() 

The routing should somehow search for /{controller} and then take the first available action from the list above as default action, e.g.:

/ControllerA     -> ControllerA.List()
/ControllerB     -> ControllerB.Draw()
/ControllerC     -> ControllerC.ViewSingle()
/ControllerD     -> ControllerD.Draw()
/ControllerE     -> ControllerE.List()

Is this possible? I tried creating additional Default actions like this but couldn't get it to work:

routes.MapRoute("Default1", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "List" } 

routes.MapRoute("Default2", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "Draw" } 

routes.MapRoute("Default3", "{controller}/{action}", 
 new { controller = UrlParameter.Optional, action = "ViewSingle" } 

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

明月夜 2024-09-10 07:36:07

我认为您的默认路线有问题。这些控制器和动作参数是为了约定而存在的。如果 URL 中有控制器名称,它将路由到合适的控制器。 Index 方法也是如此。它只是为此提供了一个默认值。它们已经是可选的,这就是原因。我认为你在这里不需要路由:你可以尝试将默认函数放在 Index 方法中,它会起作用。需要明确的是:

public class ControllerA:Controller
{
    public ActionResult Index()
    {
        return List();
    }

    public ActionResult List()
    {
    //List function
    }
}


public class ControllerB:Controller
{
    public ActionResult Index()
    {
        return Draw();
    }

    public ActionResult Draw()
    {
    //Draw function
    }
}

我认为它会起作用。但是,如果您想继续创建路由,则可以像这样创建每个路由:

routes.MapRoute("Default1", "ControllerA", 
 new { controller = ControllerA, action = "List" }

请注意,ControllerA 不在花括号中,它是静态文本。因此,您为每个控制器创建路由。请记住控制器命名约定。

I think you got something wrong about the default route. Those controller and action parameters are there for convention. If URL has a controller name in it, it will route to suitable controller. Same thing is true for Index methods. It just provides a default value for that.They are already optional and that's why. I think you don't need routes here: You could try to place your default functions in Index methods and it would work.Just to be clear:

public class ControllerA:Controller
{
    public ActionResult Index()
    {
        return List();
    }

    public ActionResult List()
    {
    //List function
    }
}


public class ControllerB:Controller
{
    public ActionResult Index()
    {
        return Draw();
    }

    public ActionResult Draw()
    {
    //Draw function
    }
}

I think it would work. But if you want to go on with creating routes, you create each route like this:

routes.MapRoute("Default1", "ControllerA", 
 new { controller = ControllerA, action = "List" }

Notice that ControllerA is not in curly braces,it's a static text. So you create routes for each controller. Keep controller naming convention in mind.

黒涩兲箜 2024-09-10 07:36:07

我同意纳西尔的观点。您应该向 url 写入一个常量。 - 就像控制器名称 - 因为 MVC 无法解析此路由并且无法弄清楚您想要显示的内容。

例如。

routes.MapRoute(null, "ControllerA/{action}", 
 new { controller = "ControllerA", action = "List" } 

routes.MapRoute(null, "ControllerB/{action}", 
 new { controller = "ControllerB", action = "Draw" } 

routes.MapRoute(null, "ControllerC/{action}", 
 new { controller = "ControllerC", action = "ViewSingle" } 

I agree with Narsil. You should write a constant to url. -like controller name- 'cause MVC cannot resolve this routes and cannot figure out what you want to show.

eg.

routes.MapRoute(null, "ControllerA/{action}", 
 new { controller = "ControllerA", action = "List" } 

routes.MapRoute(null, "ControllerB/{action}", 
 new { controller = "ControllerB", action = "Draw" } 

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