如何在没有其他操作匹配的情况下为我的Controller设置“默认操作”?

时间:2023-01-29 21:13:20

Say I have the following route:

说我有以下路线:

routes.MapRoute("Default", "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = "" });

Lets also say that my controller has the following methods: Index(Int32 id) and Edit(Int32 id).

让我们说我的控制器有以下方法:索引(Int32 id)和编辑(Int32 id)。

So /MyController/Index/1 is a valid URL for that route. So is /MyController/Edit/1

所以/ MyController / Index / 1是该路由的有效URL。 / MyController / Edit / 1也是

However, if a URL is received that correctly maps to my controller but not to an existing action, how do I define a "Default Action" to execute instead of letting the MVC framework throw up an error screen?

但是,如果收到的URL正确映射到我的控制器而不是现有操作,那么如何定义要执行的“默认操作”而不是让MVC框架抛出错误屏幕?

Basically I'd like the URLs /MyController/Preview/1 and /MyController/Whatever/1 to execute an action that I specify ahead of time when the {action} token can't be mapped to an existing action on my controller.

基本上我喜欢URL / MyController / Preview / 1和/ MyController / Whatever / 1来执行我在{action}令牌无法映射到我的控制器上的现有操作时提前指定的操作。

I see that the MvcContrib project on Codeplex has an attribute that enables this for use with the ConventionController, but I'd like to keep this with pure MS ASP.NET MVC for now.

我看到Codeplex上的MvcContrib项目有一个属性,可以将它与ConventionController一起使用,但我现在想用纯MS ASP.NET MVC保留它。

I also see that Fredrik mentions a [ControllerAction(DefaultAction = true)] attribute, but I can't find mention of it anywhere except his blog (and my app won't compile when I try it in my controller).

我也看到Fredrik提到了一个[ControllerAction(DefaultAction = true)]属性,但除了他的博客之外我无法在任何地方找到它(当我在我的控制器中尝试时,我的应用程序将无法编译)。

2 个解决方案

#1


12  

You can do the following for now.

您现在可以执行以下操作。

protected override void HandleUnknownAction(string actionName) {
  //your code here.
}

Another approach is that you put a constraint on the default route so it only matches methods you know exist on the controller. Then you could have another route like so:

另一种方法是在默认路由上设置约束,使其仅匹配控制器上存在的方法。然后你可以有另一条路线:

routes.MapRoute("default-action", "{controller}/{actionName}/{id}", new {action="DefaultAction"});

Which maps to

哪个映射到

public ActionResult DefaultAction(string actionName, string id) {
  //handle default action
}

This gets you the result you're looking for.

这可以获得您正在寻找的结果。

#2


8  

Farooq Kaiser did an article on CodeProject on this topic which I found useful: Handling Unknown Actions in ASP.NET MVC

Farooq Kaiser在这个主题上做了一篇关于CodeProject的文章,我觉得它很有用:处理ASP.NET MVC中的未知操作

I particularly like the trick of creating "view only" pages (obviously error handling code should be added):

我特别喜欢创建“仅查看”页面的技巧(显然应该添加错误处理代码):

protected override void HandleUnknownAction(string actionName)
{
   this.View(actionName).ExecuteResult(ControllerContext);
}

#1


12  

You can do the following for now.

您现在可以执行以下操作。

protected override void HandleUnknownAction(string actionName) {
  //your code here.
}

Another approach is that you put a constraint on the default route so it only matches methods you know exist on the controller. Then you could have another route like so:

另一种方法是在默认路由上设置约束,使其仅匹配控制器上存在的方法。然后你可以有另一条路线:

routes.MapRoute("default-action", "{controller}/{actionName}/{id}", new {action="DefaultAction"});

Which maps to

哪个映射到

public ActionResult DefaultAction(string actionName, string id) {
  //handle default action
}

This gets you the result you're looking for.

这可以获得您正在寻找的结果。

#2


8  

Farooq Kaiser did an article on CodeProject on this topic which I found useful: Handling Unknown Actions in ASP.NET MVC

Farooq Kaiser在这个主题上做了一篇关于CodeProject的文章,我觉得它很有用:处理ASP.NET MVC中的未知操作

I particularly like the trick of creating "view only" pages (obviously error handling code should be added):

我特别喜欢创建“仅查看”页面的技巧(显然应该添加错误处理代码):

protected override void HandleUnknownAction(string actionName)
{
   this.View(actionName).ExecuteResult(ControllerContext);
}